简体   繁体   English

如何正确关闭 boost 线程

[英]How to properly close boost thread

In my code i open a boost thread for io service and want to close the thread once the while loop is over.在我的代码中,我为 io 服务打开了一个 boost 线程,并希望在 while 循环结束后关闭该线程。

Currently the appilcation crashes in the debug mode and i get this error message.目前,应用程序在调试模式下崩溃,我收到此错误消息。

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

}

As per the comment, it's not enough to simply call io_service.stop() .根据评论,仅仅调用io_service.stop()是不够的。 The thread on which the service is running must be joined to allow it to exit gracefully.必须joined运行服务的线程才能使其正常退出。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM