Will WaitForMultipleObjects change the state * of several objects?

When using WaitForMultipleObjects(... /*bWaitAll=*/FALSE ...) function will obviously change the state of the first synchronization object, which will make it return. That is, if you have (have) the signal auto-reset event , and the return value indicates that this event object called the function to return, it certainly was reset.

However, consider the case when you have several objects - here:

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

So, you will only get the first descriptor, and you don’t know if any events were noted after this index.

For objects whose state is changed, now the question is: If several objects were specified at the moment of WaitForMultipleObjects return, will only the state of the first one be changed, or all objects with an alarm be reset?

The documents indicate:

The function changes the state of some types of synchronization objects. Modification occurs only for the object or objects, the signaling state caused a return of the function.

therefore, this may indicate that it is indeed possible for several objects to change their state. However, this is slightly contrary to the statement:

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

And, in addition, this will mean that this function cannot be used with several synchronization objects (for example, with an automatic reset event, semaphores, etc.) that change their state because you always lose information.


I found expression in this answer to "Behavior of WaitForMultipleObjects when several descriptors ..." that others would conclude this (from a comment there):

WaitForMultipleObjects () checks the array of descriptors from 0 to and returns as soon as it finds a signal descriptor. Only this first reset handle was detected for an unauthorized state; others are untouched. - user82238 / # 259 at 19:27

but I would like to repeat and explicitly confirm this explicitly.


There is also an interesting discussion at CodeGuru that does not seem to shed any light on this.

+2
source share
1 answer

Well. Waddayaknow.

Comment by Raymond Chen :

When waiting for one event, only this event changes. If waiting for all events, then everything will be changed. This is what documentation means "object or objects." Singular, if you wait, plural, if you wait, that's it. - Raymond Chen

This will be consistent with the documentation, as before the paragraph containing “object or objects”, under the same subtext of Remarks , find :

When bWaitAll is TRUE, the function wait operation completes when the state of all objects is set to alarm. The function does not change the states of the indicated objects until the states of all objects have been set to the alarm.

So, to answer the question, it follows: If bWaitAll==FALSE , then only the first object (the one that was indicated by the returned index) changed its state.

+2
source

Source: https://habr.com/ru/post/911139/


All Articles