mindless.panda
mindless.panda

Reputation: 4092

How to only use a specific amount of threads in the threadpool at any given time

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++.

Upvotes: 1

Views: 431

Answers (4)

kyoryu
kyoryu

Reputation: 13065

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?

Upvotes: 0

Kiril
Kiril

Reputation: 40395

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.

Upvotes: 0

Cetra
Cetra

Reputation: 2621

It supports it via the SetMaxThreads method:

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

Upvotes: 1

Michael Haren
Michael Haren

Reputation: 108376

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.

Upvotes: 0

Related Questions