boost :: thread sleep()是什么? [英] What does boost::thread sleep() do?

查看:596
本文介绍了boost :: thread sleep()是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为boost线程工作一个小的包装类,但我真的不知道睡眠功能如何工作,这是我到目前为止:

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_thread :: sleep方法知道哪个线程休眠,例如我的线程包装器的多个实例是活动的。这是正确的实现方式吗?

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?

推荐答案

boost :: this_thread :: sleep 将睡眠当前线程。只有线程本身可以睡觉。如果您想让线程休眠,请在线程或 use interruptions

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.

更新:如果使用c ++ 11编译器到目前为止的标准库,您将可以访问 std :: this_thread :: sleep_for std :: this_thread :: sleep_until 函数。但是,没有标准的中断机制。

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.

这篇关于boost :: thread sleep()是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆