What's the difference between notify_all() and notify_one() of std::condition_variable?

27,878

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.

Share:
27,878
Yun Huang
Author by

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, 2020

Comments

  • Yun Huang
    Yun Huang over 4 years

    Currently, I am implementing a multi-thread project using std::thread in C++11. I use std::condition_variable to synchronize threads. In detail, one consumer function calls wait() member function of std::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 between notify_all() and notify_one() member functions of std::condition_variable. Which function should I use in the producer function? Thanks!

  • Yun Huang
    Yun Huang over 12 years
    Thanks, 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
    David Schwartz over 12 years
    Only 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
    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
    Steve Jessop over 12 years
    And 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 use notify_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.