HANDLE WINAPI CreateSemaphore( _In_opt_ LPSECURITY_ATTRIBUTES lpSemaphoreAttributes _In_ LONG lInitialCount, _In_ LONG lMaximumCount, _In_opt_ LPCTSTR lpName );第一个参数:安全属性,如果为NULL则是默认安全属性
BOOL WINAPI ReleaseSemaphore( _In_ HANDLE hSemaphore, _In_ LONG lReleaseCount, _Out_opt_ LPLONG lpPreviousCount );第一个参数:信号量句柄
分析:这里我们要让三个线程按顺序依次打印ABC,即当一个线程在打印A时,另外两个线程不能够打印,而且打印完A以后,接下来必须打印B。我们使用三个信号量分别控制A B C的打印,打印完A就释放B的信号量,打印完B就释放C的信号量,打印完C就释放A的信号量。具体见下面代码:
#include<string> #include<iostream> #include<process.h> #include<windows.h> using namespace std; HANDLE hsem1, hsem2, hsem3; //线程绑定的函数返回值和参数是确定的,而且一定要__stdcall unsigned __stdcall threadFunA(void *) { for (int i = 0; i < 10; i++){ WaitForSingleObject(hsem1, INFINITE);//等待信号量 cout << "A"; ReleaseSemaphore(hsem2, 1, NULL);//释放信号量 } return 1; } unsigned __stdcall threadFunB(void *) { for (int i = 0; i < 10; i++){ WaitForSingleObject(hsem2, INFINITE);//等待信号量 cout << "B"; ReleaseSemaphore(hsem3, 1, NULL);//释放信号量 } return 2; } unsigned __stdcall threadFunC(void *) { for (int i = 0; i < 10; i++){ WaitForSingleObject(hsem3, INFINITE);//等待信号量 cout << "C"; ReleaseSemaphore(hsem1, 1, NULL);//释放信号量 } return 3; } int maintest003() { //创建信号量 hsem1 = CreateSemaphore(NULL, 1, 1, NULL); hsem2 = CreateSemaphore(NULL, 0, 1, NULL); hsem3 = CreateSemaphore(NULL, 0, 1, NULL); HANDLE hth1, hth2, hth3; //创建线程 hth1 = (HANDLE)_beginthreadex(NULL, 0, threadFunA, NULL, 0, NULL); hth2 = (HANDLE)_beginthreadex(NULL, 0, threadFunB, NULL, 0, NULL); hth3 = (HANDLE)_beginthreadex(NULL, 0, threadFunC, NULL, 0, NULL); //等待子线程结束 WaitForSingleObject(hth1, INFINITE); WaitForSingleObject(hth2, INFINITE); WaitForSingleObject(hth3, INFINITE); //一定要记得关闭线程句柄 CloseHandle(hth1); CloseHandle(hth2); CloseHandle(hth3); CloseHandle(hsem1); CloseHandle(hsem2); CloseHandle(hsem3); system("pause"); return 1; }参考:http://www.cnblogs.com/TenosDoIt/p/3601252.html