#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;
HANDLE event; // event object
int eventNumber=0; // data to pass between threads
// the thread function
DWORD WINAPI ThreadProc(void* x )
{
cout << "thread started" << endl;
for(int i=0; i<10; i++)
{
eventNumber++;
SetEvent(event); // ser event
Sleep(1000); // sleep for 1 second
}
return 0;
}
// test from to call the thread function and wait for event
int main(int argc, char *argv[])
{
// creat event handle and the thread
event = CreateEvent(0,true,false,0);
CreateThread( NULL, 0, ThreadProc, NULL, 0, NULL); // create thread
while(eventNumber < 10)
{
// loop waiting for the event, reset and display data
WaitForSingleObject(event, INFINITE);
ResetEvent(event);
cout << "event number" << eventNumber << endl;
}
return 0;
}