How to stop std thread safely? How to stop std thread safely? multithreading multithreading

How to stop std thread safely?


You can pass a suitable context to your thread(s) which can contain a flag indicating whether it is time to stop. The flag could be a std::atomic<bool>. Obviously, you' d also need to set up communication to not wait indefinitely for data so you have the chance to check the flag once in a while.


I'm fairly sure that accept will exit cleanly, throwing an exception, when you close the acceptor. You should catch the exception so that the thread exits normally:

void CServerSocket::AcceptRun(boost::asio::io_service &iosrv)try {    // your loop here, unchanged} catch (std::exception const & ex) {    // perhaps log the message, ex.what()}

and then join the thread after closing the acceptor, but before destroying it:

CServerSocket::~CServerSocket(){    CLogManager::WriteLog("Stopping Server...");    m_Acceptor->close();    m_AcceptThread.join();    CLogManager::WriteLog("Server Stopped!");    // No need to do anything else with m_Acceptor, assuming it's a smart pointer}

Personally, I'd use asynchronous operations unless there were a compelling reason to use multiple threads. A single thread is much easier to deal with.


How to stop std::thread safely?

Stopping the thread safely means that you tell the thread function to stop processing (through a mechanism that is outside std::thread), then wait for the thread to stop.

The response from @DietmarKuhl tells you how to do this. Regarding the acccept being blocking, you must set an option on the socket/acceptor to expire on a timeout. When the accept call returns, you either break the loop (if your loop condition is false) or you call accept again, with a new timeout.

Your timeout value will be a compromise: a small timeout will be more computationally intensive (keep the CPU busy) while giving you a very responsive thread function (one that doesn't block much when you stop the thread).