why is std::condition_variable::notify_one blocking?

寵の児 提交于 2019-12-11 02:26:00

问题


For some reason the call signal.notify_one() blocks the current thread and doesn't return. 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.

I hope you can point me in the right direction, thank you much for your help!


回答1:


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

My best guess is that a std::condition_variable becomes invalid if a thread is killed that is waiting on that std::condition_variable.

Anyway, in order to prevent the issue, one has to call the following before the main(int argc, char** argv) returns:

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


来源:https://stackoverflow.com/questions/37107778/why-is-stdcondition-variablenotify-one-blocking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!