简体   繁体   English

Spurios唤醒和条件变量

[英]Spurios wake up and condition variables

I was reading this nice blog from A Williams: 我从A Williams读了一个不错的博客:

http://www.justsoftwaresolutions.co.uk/threading/condition-variable-spurious-wakes.html http://www.justsoftwaresolutions.co.uk/threading/condition-variable-spurious-wakes.html

and one thing bugs me: when thread wakes up because of the spurious wake does it have mutex locked? 一件事困扰着我:当线程由于虚假唤醒而唤醒时,它是否已锁定互斥锁?

boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
    the_condition_variable.wait(lock);
}

I guess so because otherwise call to .empty would be unsafe but Im not sure. 我猜是这样,因为否则调用.empty是不安全的,但我不确定。

Yes, it does have the mutex locked. 是的,它确实已锁定了互斥锁。 Basically, the mutex gets released only while the thread is blocked in the_condition_variable.wait() . 基本上,互斥锁仅在线程在the_condition_variable.wait()被阻塞时才释放。 Spurious wakeup or not, the mutex is locked everywhere else in the code that you show. 不管是否是虚假唤醒,互斥锁都将在显示的代码中锁定在其他任何地方。

From the documentation for boost::condition_variable::wait() : boost::condition_variable::wait()文档中:

Postcondition: 后置条件:

lock is locked by the current thread. lock被当前线程锁定。

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

相关问题 线程何时从condition.wait()中唤醒 - when does a thread wake up from condition.wait() 如何使用条件变量唤醒多个线程 - How to wake up multiple threads using condition variable 在不使用条件变量的情况下唤醒线程的最快方法 - fastest way to wake up a thread without using condition variable 如果存在条件/谓词,通知条件变量是否保证唤醒具有成功条件/谓词的线程? - Does notifying a condition variable guarantee the wake-up of a thread with a successful condition/predicate if one exists? 即使有谓词,condition_variable 也不会被通知唤醒 - condition_variable doesn't get notified to wake up even with a predicate 为什么只有一个锁和一个原子计数器会错误地唤醒条件变量? - Why does the condition variable wake up erroneously with only one lock and an atomic counter? 在1个线程上运行的boost :: asio :: io_service如何唤醒等待状态 - How could a boost::asio::io_service running on 1 thread wake up awaiting condition 为什么 boost::condition_variable 可以使用 pthread_cond_signal 只唤醒一个线程 - Why boost::condition_variable can use pthread_cond_signal to wake up only one thread 如果没有线程需要唤醒,是否需要获取锁并通知condition_variable? - Is it necessary to acquire the lock and notify condition_variable if no thread needs to wake up? 唤醒一个处于睡眠状态的 QThread? - Wake up a QThread that is in sleep?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM