如何正确关闭boost线程

How to properly close boost thread

在我的代码中,我为 io 服务打开了一个增强线程,并希望在 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 才能正常退出。