我不清楚这件事,有人可以证实这一点吗?多个互斥体上的Windows同步
我有以下同步问题。我有以下对象:
A. Process 1, thread 1: Read & write access to the resource.
B. Process 1, thread 2: Read access to the resource.
C. Process 2, thread 3: Read access to the resource.
而这里的准入条件:
而B或C在- A必须被阻止。
- 只有A打开时,B才能被阻塞。
- 仅当A打开时,C必须被阻止。
所以我想使用2个名为互斥:
- hMutex2 =用于满足上述条件2。
- hMutex3 =用于满足上述条件3。
- hStopEvent =停止事件(如果应用程序关闭,需要停止线程)。
所以对于一个:
HANDLE hHandles[3] = {hMutex2, hMutex3, hStopEvent};
DWORD dwRes = WaitForMultipleObjects(3, hHandles, FALSE, INFINITE);
if(dwRes == WAIT_OBJECT_0 + 2)
{
//Quit now
return;
}
else if(dwRes == WAIT_OBJECT_0 + 0 ||
dwRes == WAIT_OBJECT_0 + 1)
{
//Do reading & writing here
...
//Release ownership
ReleaseMutex(hMutex2);
ReleaseMutex(hMutex3);
}
else
{
//Error
}
对于B:
DWORD dwRes = WaitForSingleObject(hMutex2, INFINITE);
if(dwRes == WAIT_OBJECT_0)
{
//Do reading here
...
//Release ownership
ReleaseMutex(hMutex2);
}
else
{
//Error
}
对于C:
DWORD dwRes = WaitForSingleObject(hMutex3, INFINITE);
if(dwRes == WAIT_OBJECT_0)
{
//Do reading here
...
//Release ownership
ReleaseMutex(hMutex3);
}
else
{
//Error
}
有人可以证实这一点:
- 当在两个互斥体上调用WaitForMultipleObjects时,它们是否都变成了信号(或阻塞)?
- 我也需要释放这两个互斥体吗?
你想要一个“读写器”锁。如果内存服务,则可以在不使用WaitForMultipleObjects的情况下完成。 (你可能需要WaitFSO)。我会看看我能否在今天晚些时候发现它的实现。在此之前,Bing用于“读写器锁定” – selbie