Will WaitForSingleObject
(or WaitForMultipleObjects
) cause a context switch when waiting for an event with a timeout time of zero? (Sleep(0)
will yield a threads time-slice immediately as far as I know.)
Now, regarding WaitForSingle|MultipleObject
and Event handles. If WaitFor*Object actually needs to wait, it would obviously yield the rest of its time-slice. However, there are two cases where the API does not need to wait:
WAIT_OBJECT_0
WAIT_OBJECT_0
or WAIT_TIMEOUT
Now, given this, will WaitForSingleObject
cause a thread context switch if a timeout time of zero is supplied?
Based on the Windows Research Kernel and some simple reverse-engineering of KeWaitForSingleObject
, the answer is no. Looking at the WRK, the internal timer's due time is checked before KiSwapThread
is called, making sure the function returns immediately if the object isn't signaled.
Now it seems I kind of found an answer while writing up the question -- here's what I assume from the available docs:
The MSDN states the following:
dwMilliseconds [in]
The time-out interval, in milliseconds. If a nonzero value is specified, the function waits until the specified objects are signaled or the interval elapses. If dwMilliseconds is zero, the function does not enter a wait state if the specified objects are not signaled; it always returns immediately. If dwMilliseconds is INFINITE, the function will return only when the specified objects are signaled.
I would take this to imply that it also does not enter a wait state if the objects are signaled and I would interpret "does not enter a wait state" as "no context switch happens".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With