[英]How to properly close boost thread
在我的代码中,我为 io 服务打开了一个 boost 线程,并希望在 while 循环结束后关闭该线程。
目前,应用程序在调试模式下崩溃,我收到此错误消息。
Microsoft C++ exception
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x000000000AA2F7D0.
Microsoft C++ exception:
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x000000000AA2F7D0.
template<class F>
struct Cleaner {
Cleaner(F in) : f(in) {}
~Cleaner() { f(); }
F f;
};
template<class F>
Cleaner<F> makeCleaner(F f) {
return Cleaner<F>(f);
}
int main()
{
boost::asio::io_service io_service;
server server1(io_service, 1980);
boost::thread t(boost::bind(&io_service::run, &io_service));
while( loop )
{
}
auto raii = makeCleaner([&]() { io_service.stop(); }); // trying to close the boost thread
}
根据评论,仅仅调用io_service.stop()
是不够的。 必须joined
运行服务的线程才能使其正常退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.