0
我一直在寻找一段时间,因为我无法找到正确的答案,所以我决定提问。互联网在interporcess同步中的用法
其实我有两个进程
流程1:
#include <windows.h>
#include <stdio.h>
// This process creates the mutex object.
int main(void)
{
HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
TRUE, // mutex owned
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("CreateMutex error: %d\n", GetLastError());
else
if (GetLastError() == ERROR_ALREADY_EXISTS)
printf("CreateMutex opened an existing mutex\n");
else printf("CreateMutex created a new mutex.\n");
if(WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
printf("Error while waiting for the mutex.\n");
else
printf("Mutex openned by second process.\n");
CloseHandle(hMutex);
return 0;
}
流程2:
#include <windows.h>
#include <stdio.h>
// This process opens a handle to a mutex created by another process.
int main(void)
{
HANDLE hMutex;
hMutex = OpenMutex(
MUTEX_ALL_ACCESS, // request full access
FALSE, // handle not inheritable
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("OpenMutex error: %d\n", GetLastError());
else printf("OpenMutex successfully opened the mutex.\n");
if(!ReleaseMutex(hMutex)){
printf("Error while releasing the mutex.\n")
}
CloseHandle(hMutex);
return 0;
}
所以,当我运行的第一个进程,它只是不等待第二个过程释放互斥量,但;当互斥体被创建为拥有,非信号时,它不应该等到某个进程/线程释放它,然后打印该消息?
在您等待你已经拥有一个互斥体的第一过程,所以等待将立即得到满足;在第二个过程中,您正在释放您*不拥有的互斥锁,这是非法的。我认为尤金是对的,你应该使用一个事件来代替。 –