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