What does boost::thread sleep() do?

安稳与你 提交于 2019-12-21 07:27:21

问题


I am currently working on a small wrapper class for boost thread but I dont really get how the sleep function works, this is what I have got so far:

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 is how I implemented it so far but I dont really understand how the static this_thread::sleep method knows which thread to sleep if for example multiple instances of my thread wrapper are active. Is this the right way to implement it?


回答1:


boost::this_thread::sleep will sleep the current thread. Only the thread itself can get to sleep. If you want to make a thread sleep, add some check code in the thread or use interruptions.

UPDATE: if you use a c++11 compiler with the up to date standard library, you'll have access to std::this_thread::sleep_for and std::this_thread::sleep_until functions. However, there is no standard interruption mechanism.




回答2:


sleep always affects current thread (the one that calls the method).



来源:https://stackoverflow.com/questions/1974166/what-does-boostthread-sleep-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!