c++ - 使用 C++ 在多个互斥体上调用 WaitForMultipleObjects 并设置超时

标签 c++ winapi synchronization mutex

如果我打电话WaitForMultipleObjects在多个互斥体上,如下所示:

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

并且dwRet返回为WAIT_TIMEOUThMutexes数组中的互斥体将处于什么状态?或者,换句话说,我应该对其中任何一个调用 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/

相关文章:

c++ - 如何在用户模式应用程序 C++ 中访问 AVStream 扩展相机控制属性

c# - 任何防止 Windows 8 进入连接待机模式的 API?

c# - 将 Oracle 数据库与 SQL Azure 同步

go - 使用 goroutine 和闭包从并发函数读取并发错误

c# - 如何将 native 窗口处理程序 (HWND) 嵌入到 C# WPF 应用程序中

multithreading - 我可以在工作线程中执行 TDataSet.DisableControls 而不用 Synchronize() 包装它吗?

c++ - -MM 标志在 g++ 中有什么作用?

c++ - 如何配置 C++ Zbar 扫描仪只解码 QR 码数据类型?

c++ - 如何在不复制的情况下将 vector 的 vector 从类传递到函数?

winapi - 注册窗口类