我正在使用这样的模式:
boost::thread receiverThread;
....
stayConnected = false;
if (receiverThread.joinable()) {
receiverThread.join();
}
receiverThread
线程定期检查stayConnected
标志,当它设置为false
它应该终止。
现在我想保护主代码免受“坏”的影响receiverThread
,如果receiverThread
5 秒后没有完成,我想杀死它:
boost::thread receiverThread;
....
stayConnected = false;
if (receiverThread.joinable()) {
receiverThread.join();
// not to hang forever, if receiverThread not finished after 5 seconds, kill it
}
做这个的最好方式是什么?