多线程同步,关于Semaphore问题
我想让主线程发一个信号唤醒制定的一个线程,然后再由唤醒后的这个线程在发送另外一个信号唤醒另一个线程。
主线程:
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?
主线程:
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?
...全文
相关推荐
pc_semaphore.zip_pc_semaphore
线程间共享数据 读者写者问题 makefile
Delphi多线程之Semaphore_(信号对象).pdf
Delphi多线程之Semaphore_(信号对象).pdf
Python多线程同步Lock、RLock、Semaphore、Event实例
一、多线程同步 由于CPython的python解释器在单线程模式下执行,所以导致python的多线程在很多的时候并不能很好地发挥多核cpu的资源。大部分情况都推荐使用多进程。 python的多线程的同步与其他语言基本相同,主要包含: Lock & RLock :用来确保多线程多共享资源的访问。 Semaphore : 用来确保一定资源多线程访问时的上限,例如资源池。 Event : 是最简单的线程间通信的方式,一个线程可以发送信号,其他的线程接收到信号后执行操作。 二、实例 1)Lock & RLock Lock对象的状态可以为locked和unlocked 使用acquire()设置
Linux下多线程编程-Pthread与Semaphore的使用.doc
Linux下多线程编程-Pthread与Semaphore的使用.doc
Java多线程同步.pdf
Java多线程同步.pdf


网络编程
