简体   繁体   English

在调用 std::condition_variable::wait() 之前多次调用 std::condition_variable::notify_one()

[英]Calling std::condition_variable::notify_one() multiple times before std::condition_variable::wait() is called

I am struggling to understand the way std::condition_variable is used.我正在努力理解std::condition_variable的使用方式。 Let's say I have Producer and consumer thread and both are operating on same condition_variable .假设我有 Producer 和 Consumer 线程,并且两者都在相同的condition_variable上运行。 Producer fills the data to process very fast and calls std::condition_variable::notify_one() every time a data is pushed to container. Producer 填充数据以非常快地处理并在每次将数据推送到容器时调用std::condition_variable::notify_one() So let's say Producer pushed 10 items and called notify_one() 10 times even before consumer can process data that was added first.因此,假设生产者推送了 10 个项目并在消费者可以处理首先添加的数据之前调用了notify_one() 10 次。 Now producer thread exits so it will not call nootify_one() anymore so what will happen to consumer thread who is waiting on condition_variable ?现在生产者线程退出,所以它不会再调用nootify_one()那么等待condition_variable消费者线程会发生什么? Will previous 9 calls to notify_one() will be queued and consumer will be unblocked 9 times more?之前对notify_one() 9 次调用是否会排队并且消费者将被解锁 9 次以上?

From cppreference ,cppreference

If any threads are waiting on *this , calling notify_one unblocks one of the waiting threads.如果有任何线程正在等待 *this ,则调用 notify_one 会解除对等待线程之一的阻塞。

The thing is, it shouldn't matter.问题是,这应该无关紧要。 Any consumer using the condition_variable must check if they are allowed to proceed, that is, if there's any available data.任何使用 condition_variable 的消费者都必须检查是否允许他们继续,即是否有任何可用数据。 In the case you describe they'll find there is, so they won't be suspended and won't require the notification.在您描述的情况下,他们会发现存在,因此他们不会被暂停,也不需要通知。

Or, said in the other way, they'll be suspended when no data is available, and will wake up only after the producer adds new data and calls notify_one with any suspended consumer.或者,换句话说,当没有数据可用时,它们将被挂起,并且只有在生产者添加新数据并使用任何挂起的消费者调用notify_one后才会唤醒。

condition_variable::notify_one() and condition_variable::notify_all() do not record the number of times they've been called. condition_variable::notify_one()condition_variable::notify_all()记录它们被调用的次数。 They apply to any threads that are waiting at the time of the call.它们适用于调用时正在等待的任何线程。 Threads that wait for the condition variable later are not affected by previous calls.稍后等待条件变量的线程不受先前调用的影响。 It's up to the program to keep track of whether there's any work pending.由程序来跟踪是否有任何待处理的工作。

That's why wait() is called from a loop:这就是从循环中调用wait()的原因:

while (no_data_ready())
    myvar.wait();

That is, only wait when there's nothing to do.也就是说,只有在无事可做时才等待。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std::condition_variable wait() 和 notify_one() 同步 - std::condition_variable wait() and notify_one() synchronization std :: condition_variable :: notify_one()多次调用而无需上下文切换 - std::condition_variable::notify_one() called several times without context switching 是否有用于 std::condition_variable 的 notify_one() 队列? - Is there a `notify_one()` queue for `std::condition_variable`s? std :: condition_variable :: notify_one可重入吗? - Is std::condition_variable::notify_one reentrant? 为什么 std::condition_variable::notify_one 阻塞? - why is std::condition_variable::notify_one blocking? std::condition_variable::notify_one: 如果有些线程有错误的谓词,它会唤醒多个线程吗? - std::condition_variable::notify_one: does it wake multiple threads if some have false predicate? 如果有很多线程在等待通知,std::condition_variable::notify_one() function 通知哪一个? - If there are many threads are waiting to be notify, which one does the std::condition_variable::notify_one() function notify? 我是否需要同步std :: condition_variable / condition_variable_any :: notify_one - Do I need to synchronize std::condition_variable/condition_variable_any::notify_one condition_variable::notify_one 不会立即解除阻塞等待? - condition_variable::notify_one does not instantly unblock wait? 为什么在 std::condition_variable 中,notify_all 比 notify_one 工作得更快(在随机请求上)? - Why in std::condition_variable notify_all works faster than notify_one (on random requests)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM