// WaitForSingleObject.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <afxmt.h> CEvent g_event; void ThreadFunc1(LPVOID pParam) { ::WaitForSingleObject(g_event, INFINITE); for( int i = 0; i < 100; i++ ) { //Sleep(100); printf("ThreadFunc1:%d\n", i); ::WaitForSingleObject(g_event, 100); } } void ThreadFunc2(LPVOID pParam) { for( int i = 0; i < 5; i++ ) { Sleep(20); printf("ThreadFunc2:%d\n", i); if (i==3) { g_event.ResetEvent(); Sleep(1000); } else { g_event.SetEvent(); } } } int _tmain(int argc, _TCHAR* argv[]) { DWORD m_ThreadID[2]; g_event.ResetEvent(); ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc1,NULL,0,&m_ThreadID[0]); ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc2,NULL,0,&m_ThreadID[1]); getchar(); }
运行结果: