简体   繁体   English

为什么 std::condition_variable::notify_one 阻塞?

[英]why is std::condition_variable::notify_one blocking?

For some reason the call signal.notify_one() blocks the current thread and doesn't return.由于某种原因,调用 signal.notify_one() 会阻塞当前线程并且不会返回。 I have never heard about this behavior and I don't know how to resolve it.我从未听说过这种行为,也不知道如何解决。

{
  std::lock_guard<std::mutex> lock(_mutex);
  _exit = true; // _exit is a std::atomic<bool> 
}

std::cout << "before" << std::endl;
_signal.notify_one();
std::cout << "after" << std::endl;

_thread.join();

I'm using Microsoft Visual C++ 2015 and the code above is called during destruction.我正在使用 Microsoft Visual C++ 2015,上面的代码在销毁期间被调用。

I hope you can point me in the right direction, thank you much for your help!我希望你能指出我正确的方向,非常感谢你的帮助!

Okey, I finally was able to find the problem.好吧,我终于找到了问题所在。 To give a bit of background, I'm currently using some Poco libraries (see http://pocoproject.org/ ) and I implemented my own Poco::Channel.为了提供一些背景知识,我目前正在使用一些 Poco 库(请参阅http://pocoproject.org/ )并且我实现了自己的 Poco::Channel。 After some digging I realized that Poco keeps all channels in a static LoggingRegistry which is only freed after all remaining threads have been killed.经过一些挖掘后,我意识到 Poco 将所有通道保存在一个静态LoggingRegistry 中,只有在所有剩余线程都被杀死后才会被释放。

My best guess is that a std::condition_variable becomes invalid if a thread is killed that is waiting on that std::condition_variable .我最好的猜测是,如果正在等待std::condition_variable的线程被终止,则std::condition_variable会变得无效。

Anyway, in order to prevent the issue, one has to call the following before the main(int argc, char** argv) returns:无论如何,为了防止这个问题,必须在main(int argc, char** argv)返回之前调用以下代码:

Poco::Logger::shutdown();
Poco::LoggingRegistry::defaultRegistry().clear(); 

I encountered a similar behavior in an embedded environment.我在嵌入式环境中遇到了类似的行为。 Depending on how the condition_variables are implemented in your OS, the notifying thread might implicitly wait for the notified thread.根据 condition_variables 在您的操作系统中的实现方式,通知线程可能会隐式等待通知线程。

If your notified thread has a very low priority and is blocked from running for a while, the call to notify_one() might block as well, until the system has time to schedule the low prio task.如果您的通知线程具有非常低的优先级并且被阻止运行一段时间,则对 notify_one() 的调用也可能被阻止,直到系统有时间安排低优先级任务。

暂无
暂无

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

相关问题 为什么在 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_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() 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()有什么区别? - 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::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: 如果有些线程有错误的谓词,它会唤醒多个线程吗? - std::condition_variable::notify_one: does it wake multiple threads if some have false predicate? std :: condition_variable :: notify_one()多次调用而无需上下文切换 - std::condition_variable::notify_one() called several times without context switching
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM