In general, volatile and multithreading are orthogonal in C ++ 11. Using volatile does not add or remove data races.
In this case, m_flag = true; is sequenced before the mutex is released in the thread started by async ([intro.execution] / p14), which, in turn, is synchronized with the subsequent acquisition of the mutex in m_cv.wait(lock) ([thread.mutex.requirements.mutex] / p11, 25), which, in turn, is sequenced before the subsequent reading of m_flag . m_flag = true; therefore, inter-thread occurs earlier and therefore occurs until the next subsequent read. ([Intro.multithread] / p13-14)
Since there are no other side effects on m_flag , m_flag = true; - a visible side effect with respect to this reading ([intro.multithread] / p15), and therefore the reading should read what was saved as a visible side effect, i.e. true
A compiler that "optimizes" this condition, regardless of whether volatile , will be a mismatch.
source share