简体   繁体   中英

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.

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() . The thread on which the service is running must be joined to allow it to exit gracefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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