c++winapireturnhandlewaitformultipleobjects

WaitForMultipleObjects return value when bWaitAll is TRUE


Since some people have different interpretation of the documentation, I'm trying to clarify once and for all the return value of WaitForMultipleObjects when

  1. bWaitAll = TRUE.
  2. all handles were signaled

Based on the documnation:
Return value
WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1)
If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled.

Question

Say I have passed 5 handles to this function and all of them were signaled, is the return value WAIT_OBJECT_0?

Note

I'm trying to verify programmatically that WaitForMultipleObjects succeeded.

DWORD dwWaitForMultipleObjectsRes = WaitForMultipleObjects(dwOpenProcessCount, handles, TRUE, m_dwWaitTimeForProcToBeKilled);
if (dwWaitForMultipleObjectsRes != WAIT_OBJECT_0)
   // failed?

I want to verify the condition correctness.


Solution

  • The documentation is fairly clear that a return code from WAIT_OBJECT_0 through to WAIT_OBJECT_0 + nCount - 1 will be returned if the wait is satisfied:

    If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled.

    It doesn't specify the exact value, so no one can say for sure what it will be other than it will be within that range.

    So instead of testing if (dwWaitForMultipleObjectsRes == WAIT_OBJECT_0), you should test:

    if ((dwWaitForMultipleObjectsRes >= WAIT_OBJECT_0)
    && (dwWaitForMultipleObjectsRes < (WAIT_OBJECT_0 + dwOpenProcessCount)))
    {
        // wait satisfied, all objects signalled
    }