[英]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.