The example in this article describes the method by which the C + + setting event notification thread works, where the main thread notifies the worker to work by setting the event status to "trusted." The implementation methods are as follows:
Copy Code code as follows:
EventDemo.cpp: Defines the entry point for a console application.
//
#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;
Initialize to untrusted state
G_event =:: CreateEvent (NULL, FALSE, false,null);
Hthread = (HANDLE):: _beginthreadex (NULL, 0, threadproc,null, 0, NULL);
Control the worker thread so that the worker thread begins to work
printf ("Press AnyKey to let work thread start ...");
GetChar ();
:: SetEvent (G_event); Set to trusted state, worker thread is triggered
The worker thread will not run until it is finished.
:: WaitForSingleObject (Hthread, INFINITE);
printf ("finished...\n");
:: CloseHandle (Hthread);
:: CloseHandle (G_event);
return 0;
}
I hope this article will help you with the C + + program design.