以下代码在我的 XP SP2 机器上运行良好,但在我的 Vista 机器上运行时,对 WaitForSingleObject 的调用会无限期地等待:
HANDLE ghSemaphore;
ghSemaphore = CreateSemaphore(NULL, 0, 1, "COM_PHILOERTEL_FINA");
if (ghSemaphore == NULL) {
MessageBoxA(NULL,"Error creating semaphore","ERROR",0);
return FALSE;
}
MessageBoxA(NULL,"Semaphore created. Waiting for it to be triggered","ERROR",0);
WaitForSingleObject(ghSemaphore, INFINITE);
// got the semaphore, ready to rock
MessageBoxA(NULL,"Got the semaphore, ready to rock!","Notice",0);
这是释放信号量的线程:
ghSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, "COM_PHILOERTEL_FINA");
if (ghSemaphore == NULL) {
MessageBoxA(NULL,"Failed to open semaphore","ERROR",0);
return FALSE;
}
if (0 == ReleaseSemaphore(ghSemaphore, 1, NULL)) {
MessageBoxA(NULL,"Plugin was unable to release the semaphore","ERROR",0);
return FALSE;
}
命名信号量是最近添加的,没有任何好处。在此之前,线程只是与其匿名信号量共享 ghSemaphore。没有明显区别。有谁知道为什么这个二进制文件(在 VC6 的 XP 机器上编译,Express Edition fwiw)在 Vista 中不起作用?正如我上面所说,WaitForSingleObject 调用永远不会结束。
谢谢!