I'm reading some example code of condition_variable:
At cppreference, the notify_one() is called like this: https://en.cppreference.com/w/cpp/thread/condition_variable
{ std::lock_guard lk(m); ready = true; std::cout << "main() signals data ready for processing\n"; } cv.notify_one(); from the code above, it's clear that it's not necessary to use any mutex to call notify_one().
But at cplusplus.com, the code is like this: https://cplusplus.com/reference/condition_variable/condition_variable/
std::unique_lock<std::mutex> lck(mtx); ready = true; cv.notify_all(); It seems that a mutex should be used before calling notify_all().
I'm confused, is a mutex necessary for notify_*() function?
ready = truein both examples