By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,660 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,660 IT Pros & Developers. It's quick & easy.

reuse a threadpool worker object (not the thread)

P: n/a
(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/lib...as(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

Apr 24 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Sam
Kevin,

Try these links

http://msdn2.microsoft.com/en-us/library/ms973903.aspx
http://msdn.microsoft.com/msdnmag/is...t/default.aspx

Sam
"Kevin" <ke**********@lycos.co.ukwrote in message
news:11*********************@r35g2000prh.googlegro ups.com...
(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/lib...as(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

Apr 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.