【发布时间】:2013-12-31 01:07:58
【问题描述】:
在主线程中我这样创建信号量
My_Semaphore = CreateSemaphore(NULL,0,MAX_SEM_COUNT,name);
然后在子线程中打开它
Test = OpenSemaphore(SYNCHRONIZE, TRUE, name);
GetlastError 返回 0,表示信号量已成功打开,但是当我尝试释放它时
ReleaseSemaphore(name, MAX_SEM_COUNT, NUUL);
GetlastError 返回 5(拒绝访问)。
但如果不是,通过“OpenSemaphore”函数打开信号量,我通过调用“CreatSemaphore”(再次在子线程中)打开它,它工作正常并且没有拒绝访问,
我的代码有什么问题?
【问题讨论】:
-
除非得到指示,否则切勿致电
GetLastError。ReleaseSemaphore告诉您如何测试成功/失败以及何时调用GetLastError。 -
你从 OpenSemaphore 得到的返回值是多少?除非 OpenSemaphore 调用返回 NULL,否则不应使用 GetLastError。
-
OpenSemaphore,给了我信号量的句柄,但不能释放它!
-
SYNCHRONIZE 访问不包括调用 ReleaseSemaphore 的权限。
-
@RaymondChen,你说得对! ,但是我应该如何同时使用 OpenSemaphore 和 ReleaseSemaphore?
标签: multithreading winapi access-denied