繁体   English   中英

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

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

在此示例中,将唤醒多少个等待线程:

第一线程

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

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

第二线程

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

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

第三线程 (与第二线程相同):

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

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

是否可以保证在此示例中,两个通知都将传递到不同的线程,而不是多次传递到同一线程?

即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.

(*) 请注意! 我不是在这里谈论“等待线程已经在过去的某个地方被通知,唤醒,做一些事情并再次输入condvar.wait()”的情况-当然,在这种情况下,几个notify_one()例程可以唤醒相同的线程一遍又一遍。

我说的是另一种情况

notify_one()已通知等待线程有关唤醒的信息,但是在此等待线程从内核调度程序接收到时隙并继续执行之前,已再次调用了另一个notify_one()。 尽管第二条通知尚未从第一条通知中唤醒,是否有可能再次将其传递到同一线程?

notify_one调用从notify_one解除了对一个线程的notify_one 这意味着,当第二次调用它时,它将无法解除阻塞,因为它不再被阻塞。

在标准30.5 / 3和30.5.1 / 7中对此进行了规定。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM