boost :: this_thread :: sleep_for()让我所有的线程都进入休眠状态

时间:2018-03-16 18:19:00

标签: c++ thread-sleep

void WINEvents::loop()
{
    while (true)
    {
        boost::this_thread::sleep_for(boost::chrono::milliseconds(500));

        //boost::this_thread::sleep(boost::posix_time::milliseconds(500));

        /*boost::mutex                   mutex;
        boost::condition_variable_any    condition;
        boost::unique_lock<boost::mutex> lock(mutex);
        condition.wait_for(lock, boost::chrono::milliseconds(500));*/
    }
}

void SDLplateforme::execute()
{
    WINEvents winEvents;
    boost::thread newThread(WINEvents::loop, &winEvents);

    while (true)
    {
        #####EDIT:
        auto time = Int2String(_timer->getTicks());
        SDL_SetWindowTitle(_window, time.c_str());
    }
}

int main(int argc, char *argv[])
{
    auto plateforme = make_shared<SDLplateforme>();

    boost::thread mainThread(SDLplateforme::execute, plateforme);
    mainThread.join();

    return 0;
}

我创建了两个线程:

  • SDLplateforme::execute() - &gt;不断在窗口框架中显示计时器值

  • WINEvents::loop() - &gt;使用boost::this_thread::sleep

  • 进入睡眠状态

但是,WINEvents::loop()中的睡眠调用似乎也使另一个线程处于睡眠状态,因为文本显示的间隔等于睡眠参数(此处为500ms),而应该每帧更新一次。

boost::this_thread::sleep()是否只应该在当前线程中休眠? 如果是的话,我做错了什么?如果不是,我该如何实现?

0 个答案:

没有答案