samephore1:
#include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[100] = "haihualovefang"; void main() { HANDLE hsem = CreateSemaphoreA(NULL, 0, 1, name); printf("创建成功"); char ch = getch(); //ReleaseMutex(mutex);// ReleaseSemaphore(hsem, 1, NULL); printf("触发信号量"); CloseHandle(hsem); }
samephore2:
#include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[100] = "haihualovefang"; void main() { HANDLE hsem = OpenSemaphoreA(SEMAPHORE_ALL_ACCESS, TRUE, name);; if (hsem == NULL) { printf("打开失败"); system("pause"); return; } printf("等待-------"); DWORD res = WaitForSingleObject(hsem, 20000); switch (res) { case WAIT_OBJECT_0: printf("收到信号-------"); break; case WAIT_TIMEOUT: printf("超时没有收到-------"); break; case WAIT_ABANDONED: printf("另外一个进程意外终止-------"); break; default: break; } CloseHandle(hsem); system("pause"); }
时间: 2024-10-12 20:45:16