3 ms·
From my other comment here, I think you have a race condition or something like that in your code, or as the peer comment suggests scheduling contention on the
by jlokier 6y ago
From my other comment here, I think you have a race condition or something like that in your code, or as the peer comment suggests scheduling contention on the CPU, which is causing this effect in your test.
For a real-world measurement, see:
>> "Windows event object was used but it takes 6-7us from SetEvent() to WaitForSingleObject() return on my machine
The person measured 6-7 microseconds.
The system timer is not interrupting that fast.
- vardump 6y agoSetEvent/WaitForSingleObject is a thread yield from SetEvent thread to the waiting thread. Scheduler does that when SetEvent is called. So microsecond level waiting time is expected. You don't need to wait for timer tick. IOW, parent was talking about timer wait, not event wait. Timer events are only processed at timer interrupt ticks.
- jlokier 6y ago> IOW, parent was talking about timer wait, not event wait. Timer events are only processed at timer interrupt ticks. No they weren't. Read parent again, my emphasis added: >> you can signal on an event from the receiver thread, and it doesn't wake up the waiting threads instantaneously, even if they are waiting on a kernel-level object. >> The WaitForObject will only return at the next interrupt of a system timer.....so exactly at the same point when a Sleep(1) would have woken up too. There is no benefit to using the event architecture, because for those sub-16ms events it doesn't actually bypass the constraint of the system timer interrupt. They described their threads in another comment. There's a receiver thread which receives network packets, and sends them internally to processing threads. The processing threads use Sleep(1) in a polling loop, because they believe the receiving thread cannot send an event to wake a processing thread faster than Sleep(1) waits anyway, and they believe the system would need special interrupt hardware to make that inter-thread wakeup faster. IOW, they are using a timer polling loop only because they found non-timer events take as long to wake the target thread as having the target thread poll every 16ms using Sleep(1). But that's not how Windows behaves, if the application is coded correctly and the system is not overloaded, (and assuming there isn't a Windows bug). My hunch is a race condition in the use of SendEvent/WaitForMultipleObjects in the test which produced that picture, because that's an expected effect if there is one. But it could also be CPU scheduler contention, which would not usually make the wake happen at the next timer tick, but would align it to some future timer tick in future, so the Sleep(1) version would show no apparent disadvantage. If it's CPU contention, there's probably significant latency variance (jitter) and therefore higher worst-case latency due to pre-emption.