I know in Java visibility of a member is not guaranteed when accessing it from another thread.
The meaning is the accessing thread will maybe see a stole value of the member (becuase the cache has not been flushed to main memory yet).
I wonder if that is the case for C++ too? (also in C++11?)
If so, how do you solve this problem in C++? (In Java, you can use the synchronized keyword).
You can use
std::atomic<T>
as type of the member. This guarantees a set of atomic operations, like fetch and increment. This is general much better than adding a mutex, as these operations are implemented with special atomic instructions of the CPU