Example of waiting for Event Objects by associating them with a I/O Completion Port (IOCP), effectively lifting MAXIMUM_WAIT_OBJECTS limit of WaitForMultipleObjects(Ex) API.
Sometimes programs need to wait for a lot of objects. But WaitForMultipleObjects(Ex) APIs are limited to 64 (MAXIMUM_WAIT_OBJECTS). To work around this limit, programs have to resolve to various complicated solutions, like starting multiple threads, refactor their logic, etc.
Up until Windows 8 the Thread Pool API wait operation too used multiple threads. This is inefficient so it was refactored to use internal NtAssociateWaitCompletionPacket call.
You can now wait for thousands of events, but only in a threadpool. If your program for some reason can't (isn't thread-safe, or uses own IOCP), you were out of luck.
The full solution is to use that internal, barely documented, APIs. This repository shows how.
- win32-iocp-events.h and win32-iocp-events.cpp abstracts the NT functions
- example.cpp shows how are they used by processing 2048 events by a single thread
- The API supports waiting for Semaphores, Threads and Processes, not just Events.