Issue with WaitForMultipleObjects()

  • Thread starter Thread starter Harsha_MR
  • Start date Start date
H

Harsha_MR

Guest
How do we use WaitForMultipleObjects() for events. For me, WaitForMultipleObjects() waits forever when bWaitAll is True.

I have 2 events 'Start_Event' and 'End_Event' and I need both the events to be signaled. If I make bWaitAll as False, I do get 'Start_Event' signaled properly. What is the issue with bWaitAll = True.

#include <windows.h>
#include <stdio.h>
HANDLE ghEvents[2];
DWORD WINAPI ThreadProc(LPVOID);

int main(void)
{
ghEvents[0] = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
TEXT("Start_Event")); // unnamed object

ghEvents[1] = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
TEXT("End_Event")); // unnamed object

hThread = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE)ThreadProc,
NULL, // no thread function arguments
0, // default creation flags
&dwThreadID); // receive thread identifier

// Wait for the threads to signal all of the event objects
dwEvent = WaitForMultipleObjects(
2, // number of objects in array
ghEvents, // array of objects
TRUE, // wait for all object --- I Need this to be TRUE to wait for signals from all events
INFINITE); // five-second wait

switch (dwEvent)
{

case WAIT_OBJECT_0 + 0:
printf("First event was signaled.\n");
break;


case WAIT_OBJECT_0 + 1:
printf("Second event was signaled.\n");
break;

case WAIT_TIMEOUT:
printf("Wait timed out.\n");
break;

// Return value is invalid.
default:
printf("Wait error: %d\n", GetLastError());
ExitProcess(0);
}

return 0;
}

DWORD WINAPI ThreadProc(LPVOID lpParam)
{
// lpParam not used in this example
UNREFERENCED_PARAMETER(lpParam);

PulseEvent(ghEvents[0];
PulseEvent(ghEvents[1];
return 0;
}

Continue reading...
 
Back
Top