Why does WaitForMultipleObjects fail with multiple thread handles? Why does WaitForMultipleObjects fail with multiple thread handles? multithreading multithreading

Why does WaitForMultipleObjects fail with multiple thread handles?


The WaitForMultipleObjects() documentation states:

The maximum number of object handles is MAXIMUM_WAIT_OBJECTS.

The value of MAXIMUM_WAIT_OBJECTS is 64 (defined in winnt.h), so 100 handles is over the limit. However, the documentation also explains how to overcome that limit:

To wait on more than MAXIMUM_WAIT_OBJECTS handles, use one of the following methods:

  • Create a thread to wait on MAXIMUM_WAIT_OBJECTS handles, then wait on that thread plus the other handles. Use this technique to break the handles into groups of MAXIMUM_WAIT_OBJECTS.

  • Call RegisterWaitForSingleObject to wait on each handle. A wait thread from the thread pool waits on MAXIMUM_WAIT_OBJECTS registered objects and assigns a worker thread after the object is signaled or the time-out interval expires.

See the answer to this question for an example of the first technique.