c00000fd
c00000fd

Reputation: 22255

Calling WaitForMultipleObjects on multiple mutexes with timeout with C++

If I call WaitForMultipleObjects on multiple mutexes like so:

HANDLE hMutexes[5] = {...};   //All mutexes
DWORD dwRet = WaitForMultipleObjects(5, hMutexes, TRUE, 5 * 1000);

And dwRet is returned as WAIT_TIMEOUT, what state will be mutexes in the hMutexes array? Or, in other words shall I call ReleaseMutex on any of them?

Upvotes: 2

Views: 662

Answers (2)

neutrino
neutrino

Reputation: 2317

If you get a WAIT_TIMEOUT it means that no mutex was signalled, so you shouldn't call ReleaseMutex in any of them.

Upvotes: 0

Jonathan Potter
Jonathan Potter

Reputation: 37122

The docs for the WaitForMultipleObjects function state that:

When bWaitAll is TRUE, ... the function does not modify the states of the specified objects until the states of all objects have been set to signaled. For example, a mutex can be signaled, but the thread does not get ownership until the states of the other objects are also set to signaled.

Therefore you do not need to worry about this situation. If WaitForMultipleObjects returns WAIT_TIMEOUT you do not own any of the mutexes. If it returns WAIT_OBJECT_0 you own all of them.

Upvotes: 4

Related Questions