Search code examples
c++winapiwaitformultipleobjects

Wait timed out of WaitForSingleObject


I am using WaitForMultipleObject function with waitForAll parameter = true. Here using a std::vector of handle object to wait for. If this function is timed out, then how can i identify that waiting on which handle is timed out??.

if(WAIT_OBJECT != WaitForMultipleObject(vector.size(), vector.data(), true, 16000))
{
//get the event that causes the wait to time out(assume that only one object is timed out.others are successfully set.)
}

Solution

  • According to MSDN's WaitForMultipleObjects function definition:

    Return value minus WAIT_OBJECT_0 indicates the array index of the object that satisfied the wait. If more than one object became signaled during the call, this is the array index of the signaled object with the smallest index value of all the signaled objects.

    So, you just need to check: if the function succeeded - it's all OK, if not, than check what kind of handles were in the array and the ones which weren't. Thus you can figure out the problematic handle.

    Also, I suggest you take a look at the SignalObjectAndWait function. Its behaviour is different, but maybe you will find it useful in some cases.