In Java, sometimes when accessing the same variable from different threads, each thread will create its own copy of the variable, and so if I set the value of the variable in one thread to 10 and then I tried to read the value of this variable from another thread, I will not get 10 (because the second thread is reading from another copy of the variable!).
To fix this problem in Java, all I had to do is to use the keyword volatile, for example:
volatile int i = 123; Does this problem also exists in C++? If so, how can I fix it?
Note: I am using Visual C++ 2010.
std::atomicis not available in VC2010.boost::atomicis an option.volatileis (in Java). It seems you´re overestimating what it can do. And no, withoutvolatileit is not "one copy per thread".