Vas*_*ily 1 c++ condition-variable c++11
在此示例中将唤醒多少个等待线程:
第一个帖子:
void wakeUp2Threads()
{
std::unique_lock<std::mutex> lock(condvar_mutex);
condvar.notify_one();
condvar.notify_one();
}
Run Code Online (Sandbox Code Playgroud)
第二线程:
{
std::unique_lock<std::mutex> lock(condvar_mutex);
condvar.wait(lock); <- 2nd thread has entered here before 1st thread entered wakeUp2Threads.
}
Run Code Online (Sandbox Code Playgroud)
第3个帖子(与第2 个帖子相同):
{
std::unique_lock<std::mutex> lock(condvar_mutex);
condvar.wait(lock); <- 3rd thread has entered here before 1st thread entered wakeUp2Threads.
}
Run Code Online (Sandbox Code Playgroud)
有没有保证在这个例子中两个通知都会被传递到不同的线程,而不是多次传递给同一个线程?
即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.
Run Code Online (Sandbox Code Playgroud)
(*) 请注意!我不是在这里谈论场景"等待线程已经在过去的某个地方被通知,醒来,做了一些事情并再次输入condvar.wait()" - 当然,在这种情况下,几个notify_one()例程可以唤醒同一个线程一遍又一遍.
我在谈论另一个案例:
notify_one()已通知等待线程有关唤醒,但是在此等待线程从内核调度程序接收到时隙并继续执行之前 - 另一个notify_one()已再次被调用.是否有可能将第二个通知再次发送到同一个线程,而它尚未从第一个通知中唤醒?