如果我打电话WaitForMultipleObjects在多个互斥体上,如下所示:
HANDLE hMutexes[5] = {...}; //All mutexes
DWORD dwRet = WaitForMultipleObjects(5, hMutexes, TRUE, 5 * 1000);
并且dwRet
返回为WAIT_TIMEOUT
,hMutexes
数组中的互斥体将处于什么状态?或者,换句话说,我应该对其中任何一个调用 ReleaseMutex
吗?
最佳答案
WaitForMultipleObjects
函数的文档指出:
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.
因此您无需担心这种情况。如果 WaitForMultipleObjects
返回 WAIT_TIMEOUT
,则您不拥有任何互斥锁。如果它返回 WAIT_OBJECT_0
,则您拥有所有这些。
关于c++ - 使用 C++ 在多个互斥体上调用 WaitForMultipleObjects 并设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377815/