views:

159

answers:

5
+1  Q: 

Threading Model

Is there a threading model that works something like the following:

while thread = nextAvailableThread():
  thread.doWork(data)

Such that when a thread is finished it triggers nextAvailableThread() to return this thread that just finished. This model would make distributing uneven data chunks between threads much easier as you could just pass on the next chunk of data to the next available thread and produce near-optimal data distribution.

I'm particularly interested in using this in C++, but I'd be happy to here of anything like this exists in any language?

EDIT: Thread pool looks just like what I was thinking of. So, any recommendations for C++ implementations?

+3  A: 

Sounds like you are looking for a Thread pool. Here is an article with links to specific implementations in different languages (in the external links at the bottom).

EDIT: Here is a C++ example.

Yishai
+3  A: 

You can use a blocking work queue and a group of worker threads. Java has ThreadPoolExecutor and I'm sure .NET has a comparable library.

Andrew Duffy
+1  A: 

Try this: threadpool

aJ
Looked at it. It doesn't seem to allow parameters to be passed to work functions.
marcog
You may refer : http://stackoverflow.com/questions/796198/how-to-create-a-boost-thread-with-data
aJ
A: 

Producer/Consumer pattern is probably what you're looking for.

In the most basic configuration you need 2 concurrent components: thradsafe queue and worker thread pool. You can find good examples of these on the web.

Shing Yip
+1  A: 

You can check the Task Parallel Library if you are using .Net.

The library is really simple to use, but has lots of powerful features inside also. In it's simplest form, you would use it like this:

Parallel.For(0, 100, delegate(int i) { 
   a[i] = a[i]*a[i]; 
});

And the beautiful thing is that it would execute each task whenever a core is ready to execute. On an n-core machine, it will have only n active threads at any given moment.

If you have some time, you can see this session by Daniel Moth. Even if you are not using .Net, it is interesting to see some examples of how these problems are solved there.

Groo