WaitForSingleObject Wait Resolution

When I wait for an event without signaling using the WaitForSingleObject function, I find that in some cases the call will return WAIT_TIMEOUT less than the specified timeout period. Just switching to a call with a timeout set to 1000 ms, I saw a call return in periods of up to 990 ms (works on WinXP). I use QueryPerformanceCounter to get an independent measurement of time in the system clock, so I don’t think the drift of the clock is likely to be the answer.

This behavior does not present any practical problems for me, but I would like to better understand this. It appears to work approximately with timer resolution. Does Microsoft provide more information about the accuracy of this feature? Should I Expect Better Accuracy in Vista?

+11
winapi
May 21 '09 at
source share
1 answer

Yes, WaitForSingleObject uses timer resolution; it does not use a high resolution timer, such as QueryPerformanceCounter.

http://msdn.microsoft.com/en-us/library/ms687069(VS.85).aspx , the MSDN article in the "Waiting Functions" section applies to this:

The accuracy of the specified timeout interval depends on the resolution of the system clock. The system clock is ticking at a constant speed. If the wait interval is less than the resolution of the system clock, a timeout may wait less than the specified time period. If the wait interval exceeds one tick, but less than two, the wait may be somewhere between one and two ticks, etc.

This article also explains how to use timeBeginPeriod to increase the resolution of the system clock - but this is not recommended.

I can come up with several reasons. First, a higher resolution is not required for almost all use of WaitForSingleObject. Using a high-resolution timer will require that the kernel constantly polls the timer (not feasible, since the kernel code is not guaranteed to always work) or reprogram it frequently to generate an interrupt (since there can be several WaitForSingleObjects and most likely only one programmable interrupt )

On the other hand, there is already a synchronization source that is constantly updated with a resolution that is more than enough for WaitForSingleObject, SetWaitableTimer and Sleep.

+8
May 21 '09 at 21:31
source share



All Articles