以下代码在我的 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。无明显差异。有谁知道为什么这个二进制文件(在 XP 机器上编译的 VC6,Express Edition fwiw)在 Vista 中不起作用?正如我上面所说,WaitForSingleObject 调用永远不会结束。
谢谢!
最佳答案
我现在无法查看,但听说过,所以请尝试: 将 CreateSemaphore 的第一个参数从 NULL 更改为 SECURITY_ATTRIBUTES 的空实例
SECURITY_ATTRIBUTES dumy;
dumy.nLength = sizeof(dumy);
dumy.lpSecurityDescriptor = 0;
dumy.bInheritHandle = TRUE;
CreateSemaphore(&dumy, 0, 1, "COM_PHILOERTEL_FINA");
顺便说一下,lMaximumCount = 1 的命名信号量完全等同于命名互斥量。因此,请审查使用互斥体的可能性。
关于c++ - WaitForSingleObject 在 XP 中获取他的信号量,但在 Vista 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212227/