(Note: I\'ve added tags to this question based on where I feel will people will be who are likely to be able to help, so please don\'t shout:))
In my VS 2017 64bit p
In C++11, an unsynchronized access to a non-atomic object (such as m_lClosed
) is undefined behavior.
The standard provides all the facilities you need to write this correctly; you do not need non-portable functions such as InterlockedCompareExchange
.
Instead, simply define your variable as atomic
:
std::atomic m_lClosed{false};
// Writer thread...
bool expected = false;
m_lClosed.compare_exhange_strong(expected, true);
// Reader...
if (m_lClosed.load()) { /* ... */ }
This is more than sufficient (it forces sequential consistency, which might be expensive). In some cases it might be possible to generate slightly faster code by relaxing the memory order on the atomic operations, but I would not worry about that.