Why does `std::this_thread::sleep_for` function refuse to sleep on WSL Ubuntu?

Viewed 230

For some strange reason std::this_thread::sleep_for does not work for me on WSL Ubuntu. It just refuses to sleep for the indicated time. I would like a second opinion to find out if I'm doing something wrong or if I just hit a bug in GNU C++ std:: implementation.

USER@ABC-WORKSTATION /tmp
$ cat sleep_test.cpp
#include <chrono>
#include <thread>
#include <iostream>

int main()
{
    auto start = std::chrono::system_clock::now();
    std::time_t start_time = std::chrono::system_clock::to_time_t(start);
    std::cout << "Start: " << std::ctime(&start_time) << std::endl;

    std::cout << "Sleep... " << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(10000));

    auto end = std::chrono::system_clock::now();
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
    std::cout << "End: " << std::ctime(&end_time) << std::endl;
}
USER@ABC-WORKSTATION /tmp
$ g++ --version
g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

USER@ABC-WORKSTATION /tmp
$ g++ -o sleep_test sleep_test.cpp
USER@ABC-WORKSTATION /tmp
$ sleep_test
Start: Wed Sep  9 14:15:18 2020

Sleep...
End: Wed Sep  9 14:15:18 2020

USER@ABC-WORKSTATION /tmp
$

As shown above, there was no 10000 msec sleep, and the code executed instantaneously!

0 Answers
Related