多线程同步,关于Semaphore问题

huangweibuct huangweibuct 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想让主线程发一个信号唤醒制定的一个线程,然后再由唤醒后的这个线程在发送另外一个信号唤醒另一个线程。

主线程:
A
{
...

h1 = CreateSemaphore( NULL, cMax,cMax, NULL); // unnamed semaphore

printf("signal");
if (hSemaphore == NULL)

{

printf("CreateSemaphore 1 error: %d\n", GetLastError());

}

if (!ReleaseSemaphore(h1,1, NULL) )
{
printf("Release h1 error: %d\n", GetLastError());
}
Sleep(3000);
CloseHandle(h1);
..
}

需要唤醒的第一个线程
B
{
...

DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(h1,0L);

switch (dwWaitResult)
{
case WAIT_OBJECT_0:
{
...
h2 = CreateSemaphore(NULL,cMax,cMax,NULL);
if (h2 == NULL)

{

printf("CreateSemaphore 2 error: %d\n", GetLastError());

}
if (!ReleaseSemaphore(h2,1, NULL) )
{
printf("Release h2 error: %d\n", GetLastError());
}
Sleep(3000);
CloseHandle(h2);
}


...
}

B要唤醒的线程
C
{
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(hRecvSemaphore,0L);

switch (dwWaitResult)
{

case WAIT_OBJECT_0:
{
...

}

break;
}
...


可是为什么Release h2 error?
...全文
评论 38
收藏 已收藏
分享
返回顶部
huangweibuct
网络编程 18,356 成员 64,161 内容