当系统处于睡眠模式时,分离线程睡眠表现得很奇怪

时间:2015-10-03 16:06:48

标签: c++ c++11

我有下面的代码。当我运行它并且系统进入睡眠模式时,该功能似乎暂停。

#include <iostream>
#include <thread>
#include <chrono>
#include <iomanip>
using namespace std;

void printData()
{
    static int x;
    std::cerr<<"Thread started";
    while(1)
    {
        std::this_thread::sleep_for(std::chrono::seconds(10));
        auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
        std::cerr<<" Printing at time "<<std::put_time(std::localtime(&now),"%c \n");
    }

}

int main()
{
    std::thread timeThread(printData);
    timeThread.detach();
    std::this_thread::sleep_for(std::chrono::seconds(10000000));
    return 0;
}

我得到了像

这样的输出
 Printing at time Sat Oct  3 21:19:34 2015 
 Printing at time Sat Oct  3 21:19:44 2015 
 Printing at time Sat Oct  3 21:19:54 2015 
 Printing at time Sat Oct  3 21:20:04 2015 
 Printing at time Sat Oct  3 21:20:43 2015 
 Printing at time Sat Oct  3 21:21:25 2015 
 Printing at time Sat Oct  3 21:21:35 2015 

正如您在第4和第5行之间看到的那样,错过了约40秒。那是在我的笔记本电脑进入睡眠模式期间。

0 个答案:

没有答案
相关问题