MFC 线程学习之事件对象

来源:互联网 发布:java多线程计算器 编辑:程序博客网 时间:2024/06/08 08:08

// 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;}


0 0