WairForSingleObject用法,在多线程里面通过事件通知

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

#include "stdafx.h"
#include
#include
using namespace std;

HANDLE g_hEvent;

DWORD WINAPI MyThreadProc1 (PVOID pParam) ;
DWORD WINAPI MyThreadProc2 (PVOID pParam) ;

int _tmain(int argc, _TCHAR* argv[])
{
    cout<<"test WaitForSingleObject"<     g_hEvent = CreateEvent(NULL,TRUE,FALSE,_T("Test"));


    CreateThread(NULL, 0, MyThreadProc1, NULL, 0,NULL);
    CreateThread(NULL, 0, MyThreadProc2, NULL, 0,NULL);


    system("pause");
 return 0;
}


DWORD WINAPI MyThreadProc1 (PVOID pParam)
{
    cout<<"MyThreadProc1 come in"<

    //DWORD dwReturn = WAIT_OBJECT_0;
    DWORD dwReturn = WaitForSingleObject(g_hEvent, INFINITE);
    if(WAIT_OBJECT_0 == dwReturn)
    {
        cout<<"MyThreadProc1 >>> WaitForSingleObject signaled"<     }
   

    cout<<"MyThreadProc1 leave"<     return 0;
}


DWORD WINAPI MyThreadProc2 (PVOID pParam)
{
    Sleep(10); // 为了证明只有释放信号,线程1才能执行
    Sleep(10); // 为了证明只有释放信号,线程1才能执行
    cout<<"MyThreadProc2 come in"<

    SetEvent(g_hEvent); // 释放信号
    cout<<"MyThreadProc2 >>> send g_hEvent signal"<

    cout<<"MyThreadProc2 leave"<     return 0;
}

你可能感兴趣的:(界面类)