This is probably caused by the fact that
WaitForMultipleObjects
limits the number of input handles with the nice magic constant MAXIMUM_WAIT_OBJECTS
(which happens to be 64).Here are a few examples:
- Creating a Thread Pool for MFC-Based ISAPI Extensions - this might be where it all started...
- The venerable
IsapiThreadpool
unit which originally appeared in Delphi 6. You only had to add it to your ISAPI DLL project to enjoy a nice performance boost. - Many others...
The (anti-)pattern is related to the process of shutting down the thread pool: to do this cleanly, the threads in the pool should be allowed to finish what they're doing (or just wake up if they're idle at the moment), perform any cleaning up as necessary and terminate correctly. The shutdown is usually performed in two steps:
- Send a shutdown signal (completion key) to each thread. Each thread in the pool calls
- Wait for all threads to terminate. The shutdown is not complete before all threads actually had a chance to receive the signal and terminate. Only then it's safe to continue closing the IOCP, freeing memory, etc. So we absolutely have to wait for the threads to terminate. The reasoning here seems to be: Since
GetQueuedCompletionStatus
in a loop and checks for the special (application-defined) shutdown completion key to which it responds by breaking out of the loop and terminating. The shutdown procedure can therefore simply send the shutdown completion key to the IOCP as many times as there are threads, relying on the fact that exactly one thread will respond to exactly one such signal.
WaitForMultipleObjects
can only handle up to 64 threads, we can't allow more threads to be associated with the pool in the first place, can we? Well, there's no need to use
WaitForMultipleObjects
in Step 2. It's fairly easy to keep a counter of active threads in the pool (interlocked-incremented when a thread starts, interlocked-decremented when a thread is finished). When the counter reaches zero (no more active threads), signal an event. With only one event to wait for, you can use WaitForSingleObject
in Step 2.