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

[亡魂溺海] 提交于 2020-02-17 22:42:31

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/9015748/whats-the-difference-between-notify-all-and-notify-one-of-stdcondition-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!