我目前正在为 boost 线程开发一个小型包装类,但我并不真正了解 sleep 功能是如何工作的,这是我目前所得到的:
BaseThread::BaseThread(){
thread = boost::thread();
bIsActive = true;
}
BaseThread::~BaseThread(){
join();
}
void BaseThread::join(){
thread.join();
}
void BaseThread::sleep(uint32 _msecs){
if(bIsActive)
boost::this_thread::sleep(boost::posix_time::milliseconds(_msecs));
}
这是我到目前为止的实现方式,但我真的不明白静态 this_thread::sleep 方法如何知道如果我的线程包装器的多个实例处于事件状态时要休眠哪个线程。这是实现它的正确方法吗?
最佳答案
boost::this_thread::sleep将休眠当前线程。只有线程本身可以进入休眠状态。如果要让线程休眠,请在线程中添加一些检查代码或 use interruptions 。
更新:如果您使用带有最新标准库的 c++11 编译器,您将可以访问 std::this_thread::sleep_for和 std::this_thread::sleep_until功能。但是,没有标准的中断机制。
关于c++ - boost::thread sleep() 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1974166/