//global variables
HANDLE g_hEvent = NULL;
//local variables
HANDLE hThread;
DWORD dwThreadID;
BOOL seteventStatus;
//create auto-reset event
g_hEvent = CreateEvent (NULL, // no security attributes
FALSE, //auto-reset event
FALSE, //initial state non-signaled
NULL); //name of the event object
If (g_hEvent)
//print error message
//exit (-1)
//create child threads
for (i=0; i<3; i++)
{
hThread=CreateThread(NULL,0,foo,(LPVOID)i,0,&dwThreadID);
if (hThread)
printf ("Thread launched successfully\n");
else
//print error message and exit (-1)
}
Sleep (1000);
//signal for auto reset event
seteventStatus = SetEvent (g_hEvent);
if (!seteventStatus)
//print error message for failing in calling set event and
exit (-1)
//wait for child threads to exit
DWORD dwCThd = WaitForMultipleObjects (3, //count of
objects
hThread, //thread handle
TRUE, //wait for all
INFINITE); //time out interval
If (dwCThd != WAIT_OBJECT_0)
//Print error message and exit (-1)
//close handles
CloseHandle (hEvent);
//close child thread handles
for (i=0; i<3; i++)
CloseHandle (hThread[i]);
//end of main function
/************
* function foo
*************/
DWORD WINAPI foo (LPVOID num)
{
for( j = 0; j<3; j++)
{
//wait for single object
DWORD dwRes = WaitForSingleObject (hEvent, INFINITE);
If (dwRes == WAIT_OBJECT_0)
//print wait for set event succeeded
else
//print error
// do something
}
return 0;
}
|