A generic atomic variable is not published properly unless it is modified under a mutex

I am reading about std :: condition_variable at http://en.cppreference.com/w/cpp/thread/condition_variable , and I do not understand this:

Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the thread wait modification.

Why is a generic atomic variable not properly published if it is not modified under the mutex? How to understand this statement?

On another page http://en.cppreference.com/w/cpp/atomic/atomic there is a statement that seems to contradict the first statement:

If one thread writes to an atomic object and another thread reads from it, the behavior is well defined (see memory model for details on data races)

+5
source share
1 answer

Consider the following example:

std::atomic_bool proceed(false); std::mutex m; std::condition_variable cv; std::thread t([&m,&cv,&proceed]() { { std::unique_lock<std::mutex> l(m); while(!proceed) { hardWork(); cv.wait(l); } } }); proceed = true; cv.notify_one(); t.join(); 

Here, the atomic shared proceed data proceed modified without using a mutex, after which the notification is sent to the condition variable. But it is possible that at the moment the notification is sent, the stream t does not wait on cv : instead, it is inside hardWork() , checking proceed immediately before that and finding it false. Notification skipped. When t completes hardWork , it will resume the wait (presumably forever).

If the main thread blocked the mutex before changing the proceed general data, the situation could have been avoided.

I think this means the situation when you say: "Even if the general variable is atomic, it must be modified under the mutex in order to correctly publish the modification of the waiting thread."

+6
source

All Articles