views:

30

answers:

1

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.

A: 

I'm not a C++ programmer, so I'll give you some half-way pseudo code for it

tcount = 0
maxproc = 4
while queue_item = queue.get_next() # depends on implementation of queue
                                    # may well be: 
                                    # for i=0; i<queue.length; i++
    while tcount == maxproc
        wait 0.1 seconds # or some other interval that isn't as cpu intensive 
                         # as continously running the loop
    tcount += 1 # must be atomic (reading the value and writing the new 
                # one must happen consecutively without interruption from 
                # other threads). I think ++tcount would handle that in cpp.
    new thread(worker, queue_item)

function worker(item)
    # ...do stuff with item here...
    tcount -= 1 # must be atomic
Tor Valamo
This may not met my requirements- thanks anyway
SamG
Actually it's just about as effective as you can get it. It limits it to 4 worker threads, and it executes the next item within the given wait time as soon as any thread becomes available. The only thing is that the worker thread has to handle an external variable, which may break a few "nice conventions", but just comment it to make it clear, and it's a nice efficient "hack".
Tor Valamo