I encountered yet another problem with std::thread
& this time while applying std::move
to swap 2 values. My code says :-
#include <iostream>
#include <thread>
using namespace std;
void swapno (int &&a, int &&b)
{
int temp=move(a);
a=move(b);
b=move(temp);
}
int main()
{
int x=5, y=7;
cout << "x = " << x << "\ty = " << y << "\n";
// swapno (move(x), move(y)); // this works fine
thread t (swapno, move(x), move(y));
t.join();
cout << "x = " << x << "\ty = " << y << "\n";
return 0;
}
Output :-
x = 5 y = 7
x = 5 y = 7
Now what's wrong in this method ? Why is such the code showing such a behaviour ? How do I correct it ?