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 in several examples:
ReportEventAsCompletion set of APIs
implemented in win32-iocp-events.cpp are direct simple abstractions of the NT functions.
These offer full control and are proper solution if the application already uses IOCPs.
- example.cpp shows how are they used directly by processing large number of events on a single thread
WaitForUnlimitedObjectsEx.h
is almost direct replacement of WaitForMultipleObjectsEx, but quite inefficient as the IOCP and associations are rebuilt for each call.
There is small optimization: early exit, when any of the objects is already signalled, with randomized order to make it more fair.
- example-WaitForUnlimitedObjectsEx.cpp shows the straightforward usage
UnlimitedWait.h
is the most efficient way to use this facility along the lines of WaitForMultipleObjectsEx. But instead you build a UnlimitedWait object,
add event handles, and then repeatedly retrieve signals using a single call. It also allows user to set up callback functions.
- example-UnlimitedWait.cpp shows how to construct and use of the batch retrieval
- Implementations provided are experimental, not thoroughly tested, and certainly not ready for production!
- These implementations have different semantics to WaitForMultipleObjectsEx. Most importantly signalled object statuses are not coalesced.
- The API supports waiting for Semaphores, Threads and Processes, not just Events.
- The API does NOT support acquiring Mutexes.
- The API does NOT support waiting for ALL object to be set at the same time.