win32练习|win32练习-信号量

// Semaphore.cpp : 定义应用程序的入口点。
//


#include "stdafx.h"
#include "Semaphore.h"
#include
#include 【win32练习|win32练习-信号量】


#define MAX_LOADSTRING 100


HANDLE Semaphore;


DWORD WINAPI Thread_1(PVOID pvParam){
WaitForSingleObject(Semaphore,INFINITE);
MessageBox(NULL,TEXT("1"),TEXT(""),MB_OK);
Sleep(1000);
ReleaseSemaphore(Semaphore,1,NULL);
return 0;
}


DWORD WINAPI Thread_2(PVOID pvParam){
WaitForSingleObject(Semaphore,INFINITE);
MessageBox(NULL,TEXT("2"),TEXT(""),MB_OK);
Sleep(2000);
ReleaseSemaphore(Semaphore,1,NULL);
return 0;
}


DWORD WINAPI Thread_3(PVOID pvParam){
WaitForSingleObject(Semaphore,INFINITE);
MessageBox(NULL,TEXT("3"),TEXT(""),MB_OK);
Sleep(3000);
ReleaseSemaphore(Semaphore,1,NULL);
return 0;
}


DWORD WINAPI Thread_4(PVOID pvParam){
WaitForSingleObject(Semaphore,INFINITE);
MessageBox(NULL,TEXT("4"),TEXT(""),MB_OK);
Sleep(4000);
ReleaseSemaphore(Semaphore,1,NULL);
return 0;
}


DWORD WINAPI Thread_5(PVOID pvParam){
WaitForSingleObject(Semaphore,INFINITE);
MessageBox(NULL,TEXT("5"),TEXT(""),MB_OK);
Sleep(5000);
ReleaseSemaphore(Semaphore,1,NULL);
return 0;
}


int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTRlpCmdLine,
intnCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

Semaphore = CreateSemaphore(NULL,2,3,NULL);
HANDLE H[5];
H[0] = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Thread_1,NULL,NULL,NULL);
H[1] = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Thread_2,NULL,NULL,NULL);
H[2] = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Thread_3,NULL,NULL,NULL);
H[3] = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Thread_4,NULL,NULL,NULL);
H[4] = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Thread_5,NULL,NULL,NULL);
WaitForMultipleObjects(5,H,TRUE,INFINITE);
return 0;
}

    推荐阅读