-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Higher-precision sleep on Windows #43376
Comments
I don't believe this will actually let you sleep for periods less than 1ms. See: https://stackoverflow.com/questions/7827062/is-there-a-windows-equivalent-of-nanosleep?rq=1 and https://stackoverflow.com/questions/85122/how-to-make-thread-sleep-less-than-a-millisecond-on-windows |
This does not affect the API of |
On my current Windows 10 machine, I also translated the gist to Rust using the The first solution using The second solution is much more complicated, because it does not guarantee a minimum sleep time. This means we would need additional code that checks for that. Great resource for further reading: https://randomascii.wordpress.com/2020/10/04/windows-timer-resolution-the-great-rule-change/ |
cc @ChrisDenton for recommendations here. |
I'm not entirely convinced we should be setting To be clear, I'm not totally against it in principle. I'm just uncertain if this is practical for the standard library. |
@ChrisDenton I agree that we shouldn't call |
Ok so the question is if using So I don't feel like this is a truly higher precision sleep, even if it sometimes wakes early. I would however revise my opinion if there were some projects that can show switching from sleep to waitable timers helped in some real way but I don't feel like this is worth it otherwise. |
Go uses If The advantage would be a slightly better accuracy up to ~1ms (from ~15.6ms). That's good enough to make it viable for e.g. games with 60 fps. To use it kind of safely we would need an additional function that the user has to call explicitly (opt-in e.g. in fn main) and calls Maybe the documentation can be updated to reflect the currently known limits on Windows. |
Hm, I've been looking in to the The flag still has yet to be documented (a recurring theme for newer changes) but it's in public headers and has been around for years at this point. I think it would be great if anyone wants to make a PR that creates a waitable timer using this flag, at the very least for testing purposes. Though it would need a fallback for if |
I never contributed to Rust and would first need some time to figure that out (and find some spare time). If someone is faster or wants to test it out independently, I shared my example project with some comments/links to documentation here. |
Right now, the sleep method uses
Sleep
on Windows, which only has millisecond precision. Even though the function explicitly allows rounding up to higher ticks, it would be nice to allow higher-precision timers.After a quick search I was able to find that the
SetWaitableTimer
function (used in this gist) supports much higher precision timing, so, this is theoretically possible. I'm not particularly good at Windows programming, though, so, I shouldn't be doing this myself.The text was updated successfully, but these errors were encountered: