MFC 线程学习之事件对象

MFC 线程学习之事件对象_第1张图片

// EVENT.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "windows.h"

int number=1;
HANDLE hEvent;

unsigned long _stdcall ThreadProc1(void* lpParameter)
{
while(number<100)
{
	WaitForSingleObject(hEvent,INFINITE);
	printf("线程1当前计数:%d\n",number);
	number++;
	Sleep(100);
	SetEvent(hEvent);
}
return 0;
}

unsigned long _stdcall ThreadProc2(void* lpParameter)
{
while(number<100)
{
	WaitForSingleObject(hEvent,INFINITE);
	printf("线程2当前计数:%d\n",number);
	number++;
	Sleep(100);
	SetEvent(hEvent);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
	HANDLE hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
	hEvent=CreateEvent(NULL,FALSE,TRUE,(LPCWSTR)"event");
	CloseHandle(hThread1);
	CloseHandle(hThread2);
	while(TRUE)
	{
	 ;
	}
	return 0;
}


你可能感兴趣的:(MFC 线程学习之事件对象)