Does WaitForMultipleObjects() reset all auto-reset events?
Asked Answered
D

1

6

I've got an event loop that's waiting on several auto-reset events. The events were all initialized into array eventHandles_ with CreateEvent(NULL, false, false, NULL).

while (true)
{
    DWORD waitResult = WaitForMultipleObjects(3, eventHandles_, false, INFINITE);
    switch (waitResult)
    {
    case WAIT_OBJECT_0 + 0:
        //handle event...

    case WAIT_OBJECT_0 + 1:
        //handle event...

    case WAIT_OBJECT_0 + 2:
        //handle event...
    }
}

My question: if event 1 and 2 occur simultaneously, the loop will process WAIT_OBJECT_0 + 1 because it's first. But will event 2 remain signaled when the loop comes around again? Or does it get reset automatically?

Deport answered 19/3, 2012 at 19:0 Comment(1)
In general, it's just best to try to avoid such constructs. I would have each signaling thread push a suitable index, delegate or whatever onto a thread-safe queue and signal a single semaphore. This designs around the issue - the behaviour is entirely known and predictable, no events will ever get lost even if one thread signals more than once, there is no 64-handle restriction and the number of signaling theads is not fixed at compile time.Prohibitory
G
7

"...modification occurs only for the object or objects whose signaled state caused the function to return..."

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx

And from the mouth of one Raymond Chen:

If waiting for one event, then only that event is modified. If waiting for all events, then all are modified. That's what the documentation means by "object or objects". Singular if wait-any, plural if wait-all.

Gemmule answered 19/3, 2012 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.