473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

reuse a threadpool worker object (not the thread)

(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
1 2834
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker...
5
by: Duane Pressley | last post by:
I'm looking for someone to help me make sense of the results I'm observing when using the ThreadPool class in a COM-Interop scenario. Here's the set up: 1.. A classic ASP page instantiates and calls...
34
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; ...
2
by: Mark Hoffman | last post by:
All, My application spawns a worker process by calling BeginInvoke with an asynchronous callback method. This callback method is responsible for calling EndInvoke. No problems there; pretty much...
3
by: Trebek | last post by:
Hello group: I have a quick question. Background: I have an assembly that is responsible for handling our clients' *requests* (not a webservice/http request per se but our concept of a...
10
by: Lenn | last post by:
Hello, I have always used a certain design pattern for multithreaded Windows app; Start new worker thread from UI thread, use events to notify UI threads when something happens, update UI...
16
by: NOtcarvinSPAM | last post by:
It occurs to me that if I have a work object that gets executed via QueueUserWorkItem AND the work proc accesses only that object's instance data I STILL may have to lock because the object...
3
by: Kevin | last post by:
Using this: http://msdn2.microsoft.com/en-us/library/3dasc8as(VS.80).aspx as an example I have a question concerning the reuse of objects. In the example 10 instances of the Fibonacci class...
1
by: Shree | last post by:
On Aug 22, 1:01 pm, "Brian Stoop" <b.st...@consultant-spam.free.com> wrote: A simple way could be to keep a static counter which can be updated in the call back function "TestThread1". Make the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.