问题
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
isFALSE
, 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.
回答1:
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.
来源:https://stackoverflow.com/questions/22814435/will-waitformultipleobjects-modify-the-state-of-multiple-objects