I want to access and modify members of a class with an std::thread
.
Here is an example:
class bar {
public:
int a = 5;
void change_a() {
a = 10;
cout << "thread's a=" << a << endl;
}
};
int main( int argc, char ** argv )
{
bar object1;
std::thread t1(&bar::change_a,bar());
t1.join();
cout << "object's a=" << object1.a << endl;
return 0;
}
The result was:
thread's a=10
object's a=5
So the function in the thread modified the variable and printed it, but it obviously did not change the object1 because when I printed that, it was still the same (5).
My question is, where is the variable now 10 if its not an object and how can I work with it?