nanosleep() on Windows with Visual Studio

The POSIX functions nanosleep() and clock_gettime() useful in games and other programs needing repeatable time delay and time measurement is not available in the standard C library on Windows with Visual Studio and certain MinGW CPU architectures.

Our CLOCK_MONOTONIC and CLOCK_REALTIME implementation of nanosleep(), clock_gettime() and clock_getres() for Windows uses Waitable Timer Objects.

The clock tick for Windows is 100 ns. The practical achievable timer resolution on Windows is in the millisecond range. This presents a floor on the accuracy achievable for sleep timers. Virtual Machine running of Windows makes the sleep accuracy significantly worse.

In general across general-purpose operating systems, high accuracy sleep timer performance is not guaranteed. For high accuracy sleep timers (i.e., beyond what is needed for gaming and abstract high-level control of hardware interfaces), consider using a real-time operating system (RTOS) or a dedicated microcontroller.