reuse a threadpool worker object (not the thread)

K

Kevin

(apologies if this is a duplicate posting, but my previous seemed to
get lost in the ether...)

Using this as an example:

http://msdn2.microsoft.com/en-us/library/3dasc8as(VS.80).aspx

I have some questions about the reuse of objects in threadpooling.

In the above example a worker object (the Fibonacci class) is created
for each Fibonacci calculation that is to occur. This works well for
10 calculations as this is well within the limits of the threadpool.

However, what if you wanted to do 10,000 calculations using the
threadpool? You really wouldn't want to create 10,000 Fibonacci
objects as there would be a considerable overhead with this (Bear in
mind my real-life worker class is much bigger). Ideally what you
would do is create 30 or so Fibonacci objects and keep reusing them.
Something perhaps a little like this:

On the main thread I would create a queue of 10,000 values of 'n' (the
Nth Fibonacci number). I would create 30 (slightly more than the
capacity of the threadpool) Fibonacci objects and place them in a
'bucket' (probably also a queue or similar) and then pull n's off the
queue stick them in the Fibonacci objects and sent the objects to run
in the threadpool until the bucket was empty. When the Fibonacci
object finished its calculation it would signal to the main thread
that it had finished which would then put it back in the 'bucket' in
order to be reused and sent back into the threadpool.

That way memory usage is restricted to only a few Fibonacci objects,
and I dont have to keep creating them.

So, does this sound reasonable? Does anyone know of any articles or
posts on best practices for this kind of thing? My google searches
didnt turn anything useful up.

Thanks in advance
Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top