ahmd
October 22nd, 2010, 05:33 AM
I'm trying to better understand the workings of the WaitForMultipleObjects API. Say, I have two objects:
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //Auto reset
HANDLE hWaitableTimer = CreateWaitableTimer(NULL, FALSE, NULL); //Auto reset
then the timer is set to some time in the future:
LARGE_INTEGER li;
li.QuadPart = -100000000LL; //10 seconds in the future
SetWaitableTimer(hWaitableTimer, &li, 0, NULL, NULL, 0);
and the event can be set by some other thread, say, at a random time.
Then I have the following worker thread that waits for these events:
enum
{
EVENT_ID,
TIMER_ID,
EVENTS_COUNT,
};
HANDLE hObjects[EVENTS_COUNT];
hObjects[EVENT_ID] = hEvent;
hObjects[TIMER_ID] = hWaitableTimer;
for(;;)
{
DWORD nResObj = WaitForMultipleObjects(EVENTS_COUNT, hObjects, FALSE, INFINITE) - WAIT_OBJECT_0;
if(nResObj == EVENT_ID)
{
//Event is signaled
DoEventSignaledProcessing();
}
else if(nResObj == TIMER_ID)
{
//Timer is signaled
DoTimerSignaledProcessing();
}
else
{
//Error
}
}
So my question is, does the fact that both timer & event are auto reset create a race condition when I use them both in the WaitForMultipleObjects API? Say, in a situation when they both become signaled at the same time, so WaitForMultipleObjects() returns the index 0 of the event but will the timer's signaled state also be accounted for, or will I miss DoTimerSignaledProcessing() in this situation? I'm not very clear on this one...
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //Auto reset
HANDLE hWaitableTimer = CreateWaitableTimer(NULL, FALSE, NULL); //Auto reset
then the timer is set to some time in the future:
LARGE_INTEGER li;
li.QuadPart = -100000000LL; //10 seconds in the future
SetWaitableTimer(hWaitableTimer, &li, 0, NULL, NULL, 0);
and the event can be set by some other thread, say, at a random time.
Then I have the following worker thread that waits for these events:
enum
{
EVENT_ID,
TIMER_ID,
EVENTS_COUNT,
};
HANDLE hObjects[EVENTS_COUNT];
hObjects[EVENT_ID] = hEvent;
hObjects[TIMER_ID] = hWaitableTimer;
for(;;)
{
DWORD nResObj = WaitForMultipleObjects(EVENTS_COUNT, hObjects, FALSE, INFINITE) - WAIT_OBJECT_0;
if(nResObj == EVENT_ID)
{
//Event is signaled
DoEventSignaledProcessing();
}
else if(nResObj == TIMER_ID)
{
//Timer is signaled
DoTimerSignaledProcessing();
}
else
{
//Error
}
}
So my question is, does the fact that both timer & event are auto reset create a race condition when I use them both in the WaitForMultipleObjects API? Say, in a situation when they both become signaled at the same time, so WaitForMultipleObjects() returns the index 0 of the event but will the timer's signaled state also be accounted for, or will I miss DoTimerSignaledProcessing() in this situation? I'm not very clear on this one...