Reading the text for std::condition_variable
I've come across this sentence:
Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread.
My question is this:
What is the use for atomics if not for "lock-free code to work with PODs"?
UPDATE
Looks like there's some confusion about my question :(
The "shared variable" in the quoted text is not the same as "condition variable". See this quote from the same page:
... until another thread both modifies a shared variable (the condition), and notifies the condition_variable
Please do not answer "why we need to use a mutex with condition variables" or "how the conditional wait works" but rather provide information on how the use of a mutex "correctly publishes" the modification of an atomic to the waiting thread, i.e. whether an expression like ++counter;
(not the test like if(counter == 0)
) would need to be done under mutex?
There is no reason to use an atomic condition variable.
Even if atomics aren't useful for condition variables, they can still be useful for other use cases. A typical use case for atomics is implementation of a lock free queue.