samedi 3 octobre 2015

Detached thread sleep behaves weird when system is in sleep mode

I have the below piece of code. When I run it and at the point system enters the sleep mode the function seems to take a pause.

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

I get an output like

 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 

As you can see between the lines 4 and 5 about 40 seconds is missed. That is during the time my laptop went in to sleep mode.

Aucun commentaire:

Enregistrer un commentaire