简体   繁体   English

std :: condition_variable :: notify_one()多次调用而无需上下文切换

[英]std::condition_variable::notify_one() called several times without context switching

How many waiting threads will wake up in this example: 在此示例中,将唤醒多少个等待线程:

1st thread : 第一线程

void wakeUp2Threads()
{
    std::unique_lock<std::mutex> lock(condvar_mutex);

    condvar.notify_one();
    condvar.notify_one();
}

2nd thread : 第二线程

{
    std::unique_lock<std::mutex> lock(condvar_mutex);

    condvar.wait(lock); <- 2nd thread has entered here before 1st thread entered wakeUp2Threads.
}

3rd thread (the same as 2nd): 第三线程 (与第二线程相同):

{
    std::unique_lock<std::mutex> lock(condvar_mutex);

    condvar.wait(lock); <- 3rd thread has entered here before 1st thread entered wakeUp2Threads.
}

Is there any guarantee that in this example both notifications will be delivered to different threads, not the same thread several times? 是否可以保证在此示例中,两个通知都将传递到不同的线程,而不是多次传递到同一线程?

Ie what does notify_one() mean: 即notify_one()是什么意思:

1) notify one thread, no matter has it been already notified (but has not been woken up yet), or not. (* see note)
or
2) notify one thread, but only this one, which has not been notified yet.

(*) Pay attention! (*) 请注意! I'm not talking here about scenario "waiting thread had already been notified somewhere in the past, woken up, done some stuff and entered condvar.wait() again" - of course, in this case several notify_one() routines can wake up the same thread over and over again. 我不是在这里谈论“等待线程已经在过去的某个地方被通知,唤醒,做一些事情并再次输入condvar.wait()”的情况-当然,在这种情况下,几个notify_one()例程可以唤醒相同的线程一遍又一遍。

I'm talking about another case : 我说的是另一种情况

notify_one() has notified waiting thread about wake-up, but BEFORE this waiting thread has received time slot form the kernel scheduler and continued execution - another notify_one() has been called again. notify_one()已通知等待线程有关唤醒的信息,但是在此等待线程从内核调度程序接收到时隙并继续执行之前,已再次调用了另一个notify_one()。 Is it possible this second notification will be delivered to the same thread again, while it hasn't been woken up from first notification yet? 尽管第二条通知尚未从第一条通知中唤醒,是否有可能再次将其传递到同一线程?

The notify_one call atomically unblocks one thread. notify_one调用从notify_one解除了对一个线程的notify_one That means that when called a second time it can't unblock the same thread as it's no longer blocked. 这意味着,当第二次调用它时,它将无法解除阻塞,因为它不再被阻塞。

This is specified in the standard at section 30.5/3 and 30.5.1/7. 在标准30.5 / 3和30.5.1 / 7中对此进行了规定。

暂无
暂无

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

相关问题 在调用 std::condition_variable::wait() 之前多次调用 std::condition_variable::notify_one() - Calling std::condition_variable::notify_one() multiple times before std::condition_variable::wait() is called 是否有用于 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 wait() 和 notify_one() 同步 - std::condition_variable wait() and notify_one() synchronization 为什么 std::condition_variable::notify_one 阻塞? - why is std::condition_variable::notify_one blocking? 如果有很多线程在等待通知,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 中,notify_all 比 notify_one 工作得更快(在随机请求上)? - Why in std::condition_variable notify_all works faster than notify_one (on random requests)? std :: condition_variable的notify_all()和notify_one()有什么区别? - What's the difference between notify_all() and notify_one() of std::condition_variable? 我是否需要同步std :: condition_variable / condition_variable_any :: notify_one - Do I need to synchronize std::condition_variable/condition_variable_any::notify_one std::condition_variable::notify_one: 如果有些线程有错误的谓词,它会唤醒多个线程吗? - std::condition_variable::notify_one: does it wake multiple threads if some have false predicate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM