Seeking help with a MT design pattern

Posted by SamG on Stack Overflow See other posts from Stack Overflow or by SamG
Published on 2009-12-31T06:49:38Z Indexed on 2010/03/30 21:03 UTC
Read the original article Hit count: 468

Filed under:
|

I have a queue of 1000 work items and a n-proc machine (assume n = 4).The main thread spawns n (=4) worker threads at a time ( 25 outer iterations) and waits for all threads to complete before processing the next n (=4) items until the entire queue is processed

for(i= 0 to queue.Length / numprocs) 
        for(j= 0 to numprocs) 
        { 


                CreateThread(WorkerThread,WorkItem) 


        } 
        WaitForMultipleObjects(threadHandle[]) 

The work done by each (worker) thread is not homogeneous.Therefore in 1 batch (of n) if thread 1 spends 1000 s doing work and rest of the 3 threads only 1 s , above design is inefficient,becaue after 1 sec other 3 processors are idling. Besides there is no pooling - 1000 distinct threads are being created

How do I use the NT thread pool (I am not familiar enough- hence the long winded question) and QueueUserWorkitem to achieve the above. The following constraints should hold

  1. The main thread requires that all worker items are processed before it can proceed.So I would think that a waitall like construct above is required
  2. I want to create as many threads as processors (ie not 1000 threads at a time)
  3. Also I dont want to create 1000 distinct events, pass to the worker thread, and wait on all events using the QueueUserWorkitem API or otherwise
  4. Exisitng code is in C++.Prefer C++ because I dont know c#

I suspect that the above is a very common pattern and was looking for input from you folks.

© Stack Overflow or respective owner

Related posts about multithreading

Related posts about threads