nbonneel
nbonneel

Reputation: 3326

Multi threading independent tasks

I have N tasks, which are independent (ie., write at different memory addresses) but don't take exactly the same time to complete (from 2 to 10 seconds, say). I have P threads.

I can divide my N tasks into P threads, and launch my threads. Ultimately, at the end, there will be a single thread remaining to complete the last few tasks, which is not optimal.

I can also launch P threads with 1 task each, WaitForMultipleObjects, and relaunch P threads etc. (that's what I currently do, as the overhead of creating threads is small compared to the task). However, this does not solve the problem either, there will still be P-1 threads waiting for the last one at some point.

Is there a way to launch threads, and as soon as the thread has finished its task, go on to the next available task until all tasks are completed ?

Thanks !

Upvotes: 2

Views: 687

Answers (1)

Not_a_Golfer
Not_a_Golfer

Reputation: 49177

yes, it's called thread pooling. it's a very common practice.

http://en.wikipedia.org/wiki/Thread_pool_pattern

Basically, you create a queue of tasks (function pointers with their arguments), and push the tasks there. You have N threads running which do the following loop (schematic code):

while (bRunning) {
   task = m_pQueue.pop();
   if (task) {
      executeTask(task);
   }
   else {
    //you can sleep a bit here if you want
   } 
}

there are more elegant ways to implement it (avoiding sleeps, etc) but this is the gist of it.

Upvotes: 2

Related Questions