C++ sets the method by which an event notifies a thread to work

  • 2020-04-02 02:47:57
  • OfStack

This article illustrates the C++ method of setting the event notification thread to work, where the main thread notifies the worker thread to work by setting the event state to "trusted." The specific implementation method is as follows:

//Eventdemo.cpp: defines the entry point for the console application. & have spent < br / >
//  
 
#include "stdafx.h" 
#include <Windows.h> 
#include <process.h> 
HANDLE g_event; 
 
UINT __stdcall ThreadProc(LPVOID) 

    ::WaitForSingleObject(g_event, INFINITE); 
    printf("in threadProc...n"); 
    return 0; 

int _tmain(int argc, _TCHAR* argv[]) 

    HANDLE hThread; 
    //Initializing into an untrusted state & NBSP; < br / >     g_event = ::CreateEvent(NULL, FALSE, FALSE,NULL); 
    hThread = (HANDLE)::_beginthreadex(NULL, 0, ThreadProc,NULL, 0, NULL);
 
    //Control the worker thread to start work & NBSP; < br / >     printf("press anykey to let work thread start.."); 
    getchar(); 
    ::SetEvent(g_event); //Set to the trusted state, the worker thread is the trigger & NBSP; < br / >  
    //This is where the worker thread ends & NBSP; < br / >     ::WaitForSingleObject(hThread, INFINITE); 
    printf("finished...n"); 
    ::CloseHandle(hThread); 
    ::CloseHandle(g_event); 
    return 0; 
}

Hope that the article described in the C++ programming to help you.


Related articles: