// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<windows.h>
#include <stdlib.h>
int g_nNum = 0;
HANDLE g_hSemaphore = nullptr;
DWORD WINAPI ThreadProc(LPVOID lpParam){
for (int i = 0; i < 5;i++)
{
WaitForSingleObject(g_hSemaphore, INFINITE);
printf("%d ", g_nNum++);
ReleaseSemaphore(g_hSemaphore, 1, NULL);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
if (!(g_hSemaphore = CreateSemaphore(NULL, 1, 1, NULL)))
return 0;
CreateThread(NULL,0,ThreadProc,NULL,0,nullptr);
CreateThread(NULL, 0, ThreadProc, NULL, 0, nullptr);
system("pause");
return 0;
}
信号量
最新推荐文章于 2022-10-01 15:03:23 发布