Nabla icon indicating copy to clipboard operation
Nabla copied to clipboard

Acurrate/Precise/Stable/Cross-platform `sleep()`

Open alichraghi opened this issue 1 year ago • 1 comments

Windows

This blog post nicely explains everything we need to know about sleeping threads in windows. TLDR; i ran the benchmarks locally and all those solutions are extremely precise and has similar CPU usage except for the the new waitable timer flag CREATE_WAITABLE_TIMER_HIGH_RESOLUTION added in Windows 10 which has an stable CPU usage even in 10ms scheduler. i believe we should listen to author's advice and go with the win32 Waitable Timers API

Linux/Android

Easy choice. We use the high-resulotion sleep function (clock_nanosleep) with CLOCK_MONOTONIC flag. there's also the POSIX function nanosleep(), but it's just clock_nanosleep() with extra overhead.

WASM

std::chrono::high_resolution_clock + std::this_thread::sleep_for

alichraghi avatar Nov 15 '24 12:11 alichraghi

we will likely be sleeping for anything between 50us and 33ms, can you make win32, linux and wasm benchmarks for this and figure out the us amount of sleep when we should use a busywait rather than the API provided sleep

More importantly does Linux and WASM offer anything similar to the Win32 timers that sleep accurately ?