std::this_thread::sleep_for() sleep forever when changing OS date/time

Viewed 541

I found std::this_thread::sleep_for()/sleep_until() may sleep forever when OS datetime changed during sleep.

Here is a sample code.

#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <Windows.h>

int main()
{
    for (int i = 0; i < 1000; i++) {
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        //Sleep(500);
        std::cout << std::to_string(i) + "\n";
    }
}

When the program is running, I change the OS time a month ahead, then the program stop printing. If I switch to Windows's Sleep() API, it works as expected.

It is a bug for std::this_thread::sleep_for()/sleep_until() or do I miss something?

Note: I am using Visual Studio 2017 and Windows 10 Pro 1909.

0 Answers
Related