简体   繁体   中英

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?

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.

如果收到WAIT_TIMEOUT则意味着没有信号通知互斥体,因此您不应在其中任何一个中调用ReleaseMutex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM