join() 失败,如果在线程内部调用 io_context.run()
join() fails if io_context.run() is called inside the thread
我收到此错误(如果我使用 std::thread,则会出现类似的"避免资源死锁"错误(:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >'
what(): boost thread: trying joining itself: Resource deadlock avoided
这是导致它的的代码。调用 join(( 时会发生异常。
TCPClient::TCPClient(string EOM_delimiter)
{
EOM_delimiter_ = EOM_delimiter;
socket_ = make_unique<boost::asio::ip::tcp::socket>(io_context_);
io_context_thread_ = make_unique<boost::thread>([this]()
{
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> fake_work = boost::asio::make_work_guard(io_context_);
io_context_.run();
});
}
bool TCPClient::disconnect()
{
boost::system::error_code ignored_ec;
boost::system::error_code ec;
socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
socket_->close(ignored_ec);
io_context_.stop();
while (!io_context_.stopped()) { continue; }
io_context_thread_->join(); //this is where the error happens
return true;
}
最后,这是调用情况:
TCPClient tcp_client("n");
//sending and receiving work fine in between..
tcp_client.disconnect();
我很困惑,因为我认为这种死锁只会发生在我从线程本身或它调用的处理程序调用 join(( 时。然而,事实并非如此。我注意到如果我不从线程内部运行io_context,join(( 工作正常。
-提升 v1.66
Fudge..我无意中从处理程序调用了 disconnect(( 并因此调用了 join((。新日新开发银行 谢谢你们!
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- Seg Fault Issue C++ (file IO / getline)
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- C++ 中的文件 IO 出错。在抛出 'std::length_error' 的实例后调用的终止 what(): basic_string::调整大小
- CPP 建议更好的方法在没有开销/IO 调用的情况下进入静音状态
- 在同一语句中调用的执行 IO 的函数:未定义或未指定
- 调用WP 8(C#)中的WinRT组件时出现System.IO.FileNotFoundException错误
- 没有系统或IO调用的睡眠