[Python-Dev] Reworking the GIL

Sturla Molden sturla at molden.no
Mon Nov 2 11:28:39 CET 2009

Martin v. Löwis skrev:
> b) notice that, on Windows, minimum wait resolution may be as large as
>    15ms (e.g. on XP, depending on the hardware). Not sure what this
>    means for WaitForMultipleObjects; most likely, if you ask for a 5ms
>    wait, it waits until the next clock tick. It would be bad if, on
>    some systems, a wait of 5ms would mean that it immediately returns.
Which is why one should use multimedia timers with QPC on Windows.

To get a wait function with much better resolution than Windows' 
default, do this:

1. Set a high resolution with timeBeginPeriod.

2. Loop using a time-out of 0 for WaitForMultipleObjects and put a 
Sleep(0) in the loop not to burn the CPU. Call QPF to get a precise 
timing, and break the loop when the requested time-out has been reached.

3. When you are done, call timeBeginPeriod to turn the multimedia timer off.

This is how you create usleep() in Windows as well: Just loop on QPF and 
Sleep(0) after setting timeBeginPeriod(1).

More information about the Python-Dev mailing list