Will WaitForMultipleObjects modify the state of *multiple* objects? [duplicate]
Asked Answered
M

1

2

When using WaitForMultipleObjects(... /*bWaitAll=*/FALSE ...) the function will obviously modify the state of the first synchronization object that causes it to return. That is, if you have (had) a signaled auto-reset event, and the return value indicates that this event object caused the function to return, that surely it has been reset.

However, consider the case where you have multiple objects - here:

When bWaitAll is FALSE, this function checks the handles in the array in order starting with index 0, until one of the objects is signaled. If multiple objects become signaled, the function returns the index of the first handle in the array whose object was signaled.

So you only get back the first handle, and you do not know if any events after this index have been signaled.

For objects whose state is modified, the question now is, iff multiple objects had been signaled at the time WaitForMultipleObjects returned, will only the state of the first one be modified, or will all signalled objects have been reset?

The docs do state:

The function modifies the state of some types of synchronization objects. Modification occurs only for the object or objects whose signaled state caused the function to return.

so this would indicate that it is indeed possible for multiple objects to have their state modified. However, this slightly contradicts the statement:

... this function checks the handles in the array in order starting with index 0, until one of the objects is signaled. ...

And furthermore it would mean that it is impossible to use this function with multiple synchronization objects (like auto reset event, semaphores, etc.) that have their state modified, as you'll always loose information.


I have found a statement in this answer to "Behavior of WaitForMultipleObjects when multiple handles..." that others would conclude that (from comment there):

WaitForMultipleObjects() scans the handle array from 0 onwards and returns as soon as it finds a signalled handle. Only that first found handle is reset to the unsignalled state; the others are untouched. – user82238 / Mar 25 '09 at 19:27

but would like to re-ask and possobly confirm this explicitly.


There is also an interesting discussion over at CodeGuru, that doesn't seem to shed any light on this.

Mosenthal answered 2/4, 2014 at 14:20 Comment(8)
I asked the same question: #9776718 but the answer included the same ambiguous documentation line...Wylen
@Wylen - Indeed essentially a duplicate in fewer words and less links. Why did you accept the answer? It doesn't seem to answer anything, if you ask me :-)Mosenthal
I think I interpreted "returns the index of the first handle in the array whose object was signaled" as implying that the first handle is "the object whose signaled state caused the function to return". Plus, WaitForMultipleObjects would pretty much be useless if it didn't behave this way.Wylen
But you are right - it is essentially ambiguous. Microsoft should fix the docs.Wylen
@Wylen - "...would pretty much be useless if it didn't behave this way..." is exactly my point, but that hasn't ever stopped anyone, has it? I think we need Raymond ;-)Mosenthal
This does sound like a question right up his alley.Louislouisa
Yes, I was about to say that too :)Wylen
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.Chukchi
M
2

Well. Waddayaknow.

Comment from 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. – Raymond Chen

This would match the documentation, as prior to the paragraph containing "object or objects", under the same Remarks sub-heading, we find:

When bWaitAll is TRUE, the function's wait operation is completed only when the states of all objects have been set to signaled. The function does not modify the states of the specified objects until the states of all objects have been set to signaled.

So to answer the question, it follows: If bWaitAll==FALSE then only the first object (the one reported be the returned index) has its state changed.

Mosenthal answered 2/4, 2014 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.