C++ 当系统处于睡眠模式时,分离线程睡眠行为异常

C++ 当系统处于睡眠模式时,分离线程睡眠行为异常,c++,c++11,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) {

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

#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;
}

如你所见,在第4行和第5行之间,大约有40秒被错过了。那是在我的笔记本电脑进入睡眠模式的时候。

你是在问为什么你的电脑在没有运行的时候没有运行?在睡眠模式或注销模式下,我相信所有后台进程都会运行?在睡眠模式下,CPU会关闭,因此自然没有任何东西可以运行。只有RAM保持通电,保存进入睡眠前所做的任何数据。你是在问为什么你的计算机在没有运行的时候没有运行?在睡眠模式或注销模式下,我相信所有后台进程都会运行?在睡眠模式下,CPU关闭,因此自然没有任何东西可以运行。只有RAM保持通电,保存进入睡眠前所做的任何数据。
 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