Home > Back-end >  Order of receiving Win32 Events
Order of receiving Win32 Events

Time:03-02

I have 3 different threads that use the Windows SetEvent API to set an event each.

DWORD thFunc1(LPVOID)
{
  ...
  SetEvent(event1);
  ...
}
DWORD thFunc2(LPVOID)
{
  ...
  SetEvent(event2);
  ...
}
DWORD thFunc3(LPVOID)
{
  ...
  SetEvent(event3);
  ...
}

A 4th thread is waiting on all of these events using WaitForMultipleObjects API.

DWORD thCatch(LPVOID)
{
  ...
  DWORD ret = WaitForMultipleObjects(3, arrHandles, FALSE, INFINITE);
  ...
}

My question is twofold:

  1. If the 3 threads signal the event at almost the same timestamp, is the order of the events guaranteed to be received in the same order as they are sent?
  2. If the answer to question 1 is NO, then is there any way that this can be achieved using Windows APIs?

Any inputs would be appreciated.

CodePudding user response:

The WaitForMultipleObjects API call makes a single promise when more than one object entered the signaled state:

If multiple objects become signaled, the function returns the index of the first handle in the array whose object was signaled.

The order in which multiple objects are signaled cannot be determined from the WaitForMultipleObjects call.

If the order in which events happened is important, you will have to record that information elsewhere. You could use a singly linked list with atomic push and pop operations, and signal an event when a new entry arrived.

  • Related