Wait for variable number of events inside WaitForMultipleObjects windows

160 views Asked by At

Usually, WaitForMultipleObjects takes an argument which tells it that for how many events this method need to wait.

DWORD WaitForMultipleObjects(
              DWORD        Count,
              const HANDLE *lpHandles,
              BOOL         bWaitAll,
              DWORD        dwMilliseconds
             );

my requirement is to wait for not a fixed number of events. So, it is possible to wait for a variable number of events in the above function or what is the other way through that I can achieve this in windows.

1

There are 1 answers

1
PaulMcKenzie On

You could use a container such as std::vector:

#include <vector>
//...
int numHandles = 10;
std::vector<HANDLE> vHandles(numHandles);
//...
vHandles[0] = CreateThread(arguments);
vHandles[1] = CreateThread(arguments);
//... etc. 
WaitForMultipleObjects(vHandles.size(), vHandles.data(), TRUE, INFINITE);