lundi 6 juillet 2020

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

}

Aucun commentaire:

Enregistrer un commentaire