samephore()信号量跨线程通信

时间:2021-03-25 17:33:16

samephore1:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h> char name[] = "haihualovefang"; void main()
{
HANDLE hsem = CreateSemaphoreA(NULL, , , name);
printf("创建成功");
char ch = getch(); //ReleaseMutex(mutex);// ReleaseSemaphore(hsem, , NULL);
printf("触发信号量");
CloseHandle(hsem); }

samephore2:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h> char name[] = "haihualovefang"; void main()
{ HANDLE hsem = OpenSemaphoreA(SEMAPHORE_ALL_ACCESS, TRUE, name);;
if (hsem == NULL)
{
printf("打开失败");
system("pause");
return;
}
printf("等待-------");
DWORD res = WaitForSingleObject(hsem, );
switch (res)
{
case WAIT_OBJECT_0:
printf("收到信号-------");
break;
case WAIT_TIMEOUT:
printf("超时没有收到-------");
break;
case WAIT_ABANDONED:
printf("另外一个进程意外终止-------");
break;
default:
break; }
CloseHandle(hsem); system("pause");
}