WaitForMultipleObjects behavior when multiple signals are processed simultaneously

Given: I populate an array of descriptors with auto reset events and pass it to WaitForMultipleObjects with bWaitAll = FALSE.

From MSDN: "When bWaitAll is FALSE, this function checks the descriptors of 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 descriptor in the array whose object was the sign."

So now, if multiple signal Ill objects get the index of the first. Do I need a loop though my array to see if others have been signaling?

Right now I have a loop that goes line by line:

For ( ; ; ) { WaitForMultipleObjects(…) If (not failed) Process object that called. Remove the handle that signaled from the array. Compact the arrary. } 
+5
source share
4 answers

So now, if multiple signal Ill objects get the index of the first. Do I need to quote though my array to see if others have signaled?

Why not just go back to Wait ()? if several objects are signaled, they will still be signaled when you return. Of course, if you have a very fast bombardment of the first object in the array of the waiting object, it will starve on others; what you do is to arrange your objects in the array of the waiting object by the frequency of fire, with the least frequent being the first.

By the way, where you use infinite for (), you can use goto. Unless you really get out of the loop, the unconditional goto most correctly expresses your intentions.

+4
source

Yes. One option is that you can do WaitForSingleObject (handle, 0) on each handle, which will immediately return and indicate whether they were marked or not.

EDIT: Here's an example of pseudo code, which I mean:

 ret = WaitForMultipleObjects() if (ret >= WAIT_OBJECT_0 && ret < WAIT_OBJECT_0 + (count)) { firstSignaled = ret - WAIT_OBJECT_0; // handles[firstSignaled] guaranteed signalled!! for (i = firstSignaled + 1; i < count; i++) { if (WaitForSingleObject(handles[i], 0) == WAIT_OBJECT_0) { // handles[i] Signaled! } } } 
+5
source

Another option you can use is RegisterWaitForSingleObject . The idea is that you put the signaling state of the event in the secondary array from the callback function, and then signal the main event, which is used to wake up the main thread (which calls WaitForSingleObject in the main event).

Obviously, you will have to make sure that the secondary array is protected from access by the main thread, but it will work.

+3
source

Only the auto-reset event that completes the wait (whose index is returned) will be reset. If there is no timeout, events will be reset.

Wed https://blogs.msdn.microsoft.com/oldnewthing/20150409-00/?p=44273

0
source

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


All Articles