Returning values from thread specific code using boost threads
I'm using C++ boost library threads like this
MyThread alarm();
boost::thread thrd(alarm);
if (thrd.timed_join(boost::posix_time::seconds(timeout)))
{
    cout << alarm.modified_var << endl;
}
else
{
    cout &开发者_Go百科lt;< alarm.modified_var << endl;
}
modified_var is changed inside MyThread class but when I print it later after synchronization point i get unitialized value of modified_var. What am I doing wrong?
That is because the alarm-object is copied when passed to the thread constructor. The solution is to wrap it in a boost::ref:
boost::thread thrd(boost::ref(alarm));
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论