Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: sleep_for is dependent on system clock #609

Closed
joaomoreno opened this issue Jan 11, 2018 · 1 comment
Closed

Windows: sleep_for is dependent on system clock #609

joaomoreno opened this issue Jan 11, 2018 · 1 comment

Comments

@joaomoreno
Copy link
Contributor

Hey there! 👋

We've hit this issue in VS Code: microsoft/vscode#41136

Ultimately here's the problem: std::this_thread::sleep_for is poorly implemented in Windows and depends on the system clock. When the thread is sleeping and the system clock is changed to a previous time, the sleep takes longer than expected, apparently by as much as the system clock was changed.

The bug easily reproduces with this snippet:

#include "stdafx.h"
#include <iostream>
#include <chrono>
#include <thread>

int main()
{
	while (true) {
		std::cout << "sleeping for 500ms" << std::endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(500));
	}

    return 0;
}

Simply go back in time and one of the 500ms sleep_for calls will take much longer.


I found a comment in here which indicates that sleep_until doesn't have the same issue, though I don't really verify that. I tried using both system_clock and steady_clock to compute the sleep_until argument; the bug still happens.

Of course, using the WinAPI call Sleep works like a charm.

Would you accept a PR in which I replace sleep_for calls for Sleep calls in Windows?

@gabime
Copy link
Owner

gabime commented Jan 11, 2018

Sure. I prefer it the be implemented in the details/os.h file for obvoius reasons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants