views:

138

answers:

4

I found this question that was very useful in learning the basics of the ThreadPool.

Now my questions lies in using the ThreadPool for a series of "tasks", like the Fibonacci class, but wanting to have at most n number of these tasks executing at any one time, or basically limiting these tasks as they execute in the ThreadPool to a maximum of n threads and spawning new ones as executing tasks are completed.

Is this doable using a for loop, a task counter, and then WaitHandle::WaitAny().

Or rather would this be a bad implementation and should I be trying some other multithreaded approach?

Update:

Retagged as c++-cli, not c++.

A: 

I think you'll find a semaphore is an excellent fit for this.

A semaphore will enable you to manage access to a resource (i.e. the threadpool) in a nice, thread safe manner.

Note, though, that the threadpool is really for lightweight processing--not a lot of heavy lifting. If you're planning to do some real computationally intense things, you might consider some of the PFX (parallel framework extenstions?) in .NET, or managing your own worker threads.

Michael Haren
+1  A: 

It supports it via the SetMaxThreads method:

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setmaxthreads%28VS.80%29.aspx

Cetra
How does this interact with asynchronous threads spawned within my program from the .NET framework? Does it "count" those as well? Or simply the work items that I create and queue?
user144182
A: 

As others have pointed out SetMaxThreads will do it in C#...

For C++: in MFC there is no ThreadPool, but there are several implementations that are available out there. There is a posix version of a Threadpool and then there is the boost friendly ThreadPool. Most implementations should have a way to limit the number of threads that can run at the same time, but you'd have to check the documentation.

Lirik
What threads go against that count? Only work items I submit?
user144182
Yep, whenever you add an item to the ThreadPool it either gets "immediately" executed in a new thread or queued for execution until a thread is available.
Lirik
A: 

Probably the easiest thing to do is to wrap the ThreadPool in a class of your own. Submit jobs to your wrapper and store them in a queue. If you have less than the desired number of "jobs" running, submit one to the ThreadPool. When each of your jobs is done, signal the wrapper that it's done, and it can queue the next job in your queue.

This way, you limit your usage of the ThreadPool, without impacting any other code.

I'm not sure if that's clear enough?

kyoryu