Suspend and Resume thread (Windows, C) <p>I'm currently developing a heavily multi-threaded application, dealing with lots of small data batch to process.</p> <p>The problem with it is that too many threads are being spawns, which slows down the system considerably. In order to avoid that, I've got a table of Handles which limits the number of concurrent threads. Then I "WaitForMultipleObjects", and when one slot is being freed, I create a new thread, with its own data batch to handle.</p> <p>Now, I've got as many threads as I want (typically, one per core). Even then, the load incurred by multi-threading is extremely sensible. The reason for this: the data batch is small, so I'm constantly creating new threads.</p> <p>The first idea I'm currently implementing is simply to regroup jobs into longer serial lists. Therefore, when I'm creating a new thread, it will have 128 or 512 data batch to handle before being terminated. It works well, but somewhat destroys granularity.</p> <p>I was asked to look for another scenario: if the problem comes from "creating" threads too often, what about "pausing" them, loading data batch and "resuming" the thread?</p> <p>Unfortunately, I'm not too successful. The problem is: when a thread is in "suspend" mode, "WaitForMultipleObjects" does not detect it as available. In fact, I can't efficiently distinguish between an active and suspended thread.</p> <p>So I've got 2 questions:</p> <ol> <li><p>How to detect "suspended thread", so that i can load new data into it and resume it?</p></li> <li><p>Is it a good idea? After all, is "CreateThread" really a ressource hog?</p></li> </ol> <p><strong>Edit</strong></p> <p>After much testings, here are my findings concerning Thread Pooling and IO Completion Port, both advised in this post.</p> <p>Thread Pooling is tested using the older version "QueueUserWorkItem". IO Completion Port requires using CreateIoCompletionPort, GetQueuedCompletionStatus and PostQueuedCompletionStatus;</p> <p>1) First on performance : Creating many threads is very costly, and both thread pooling and io completion ports are doing a great job to avoid that cost. I am now down to 8-jobs per batch, from an earlier 512-jobs per batch, with no slowdown. This is considerable. Even when going to 1-job per batch, performance impact is less than 5%. Truly remarkable.</p> <p>From a performance standpoint, QueueUserWorkItem wins, albeit by such a small margin (about 1% better) that it is almost negligible.</p> <p>2) On usage simplicity : Regarding starting threads : No question, QueueUserWorkItem is by far the easiest to setup. IO Completion port is heavyweight in comparison. Regarding ending threads : Win for IO Completion Port. For some unknown reason, MS provides no function in C to know when all jobs are completed with QueueUserWorkItem. It requires some nasty tricks to successfully implement this basic but critical function. There is no excuse for such a lack of feature.</p> <p>3) On resource control : <strong>Big win</strong> for IO Completion Port, which allows to finely tune the number of concurrent threads, while there is no such control with QueueUserWorkItem, which will happily spend all CPU cycles from all available cores. That, in itself, could be a deal breaker for QueueUserWorkItem. Note that newer version of Completion Port seems to allow that control, but are only available on Windows Vista and later.</p> <p>4) On compatibility : small win for IO Completion Port, which is available since Windows NT4. QueueUserWorkItem only exists since Windows 2000. This is however good enough. Newer version of Completion Port is a no-go for Windows XP.</p> <p>As can be guessed, I'm pretty much tied between the 2 solutions. They both answer correctly to my needs. For a general situation, I suggest I/O Completion Port, mostly for resource control. On the other hand, QueueUserWorkItem is easier to setup. Quite a pity that it loses most of this simplicity on requiring the programmer to deal alone with end-of-jobs detection.</p>