What's the difference between notify_all() and notify_one() of std::condition_variable?
If there are ten threads blocked on the condition variable, for example, notify_one()
will unblock only one thread, while notify_all()
will unblock them all. In your case, you'll want to use notify_one()
so you don't wake up threads that don't have any work waiting for them.
Yun Huang
2018.01 - present, Software Engineer, Google LLC, Kirkland, WA, USA 2013.10 - 2018.01, Software Developer, Epic Systems Corporation, Verona, WI, USA
Updated on January 26, 2020Comments
-
Yun Huang over 4 years
Currently, I am implementing a multi-thread project using
std::thread
in C++11. I usestd::condition_variable
to synchronize threads. In detail, one consumer function callswait()
member function ofstd::condition_variable
to wait for task from a global task queue, another producer function generates and puts tasks into the queue. But I do not know the difference betweennotify_all()
andnotify_one()
member functions ofstd::condition_variable
. Which function should I use in the producer function? Thanks! -
Yun Huang over 12 yearsThanks, GMan. I read some documents from the internet. Just as what you said. However, typically the wait() function is used on a mutex, e.g. std::unique_lock<std::mutex> ul(m_mutexTask); while (m_lTask.empty()) { m_condTask.wait(ul); } . Then even notify_all() wake up all threads, there is only one thread could lock the mutex, right?
-
David Schwartz over 12 yearsOnly one thread will lock the mutex at a time, but they'll all return from the
wait
as soon as they get the mutex. -
Donal Fellows over 12 years@Yun: Which one to use depends really on whether any of the waiting threads can handle the thing that's being waited for. If any would do (e.g., multiple identical readers on a queue) then you use notify_one as that's definitely more efficient. If there's a more complex condition such that only one waiting thread could actually succeed with the loop condition, you've got to wake up all of them as you can't control which thread will be woken by notify_one.
-
Steve Jessop over 12 yearsAnd if there's only one waiting thread, then it makes no difference which you use. So you might as well use
notify_all
in case you ever add more waiters in future -- since waiters on a condition variable can wake up for no reason, and must be written to deal with that correctly, adding extra wakes does no harm other than perhaps to performance. Whereas if you usenotify_one
, you might accidentally write code that relies on the "right" thread being chosen. It might appear to work and pass all your tests, but then some subtle change in future causes it to stop working.