Re: ThreadPool Questions
- From: reeset <reeset@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 3 Aug 2007 14:32:02 -0700
I can definitely tell you that IO is insignificant. The record processing
itself makes up the lionshare of the "work" being done by the application.
It was part of the reason why I'd thought placing the items in a threadpool
would provide some optimizations.
You should either have a single synchronized queue or one queue for
each worker thread (still requires synchronization, but will have less
contention).
You sould have an output data pool. I'm not sure what the best
collection to use but perhaps a dictionary is best. Output data also
needs to be synchronized.
The reader thread can read items and then put them into the queue(s).
They need to be tagged with their sort order, ideally sequential
numbers starting with zero.
Worker threads all continuously take data out of the queue(s), perform
the calculations, a put results into the output pool.
The writer thread has to take stuff out of the pool but also make sure
it does so in the right order. If you have a single sequential
number, then you can just do a simple loop and block when the next
needed value is not ready.
Thanks, I think I can visualize what you are talking about. I'll see where
this takes me.
--TR
"Samuel R. Neff" wrote:
.
First, you need to determine if the calculations themselves are taking
most of the time in the loop (the non-threaded version). If the
calculations are only a small part of the loop and most of the time
required is for reading and writing files, then multithreading will
not help.
So if the calculations really are the bottleneck, then multithreading
is good but you want less synchronization between threads. The whole
point is to enable threads to perform things at their own pace. Due
to the required ordering on output, you do need some synchronization
to make sure the data comes out in the right order, but not nearly as
much as what you have.
I would suggest a produce/consumer architecture like this:
1 thread - reading thread (Main() entrypoint)
x threads - worker threads
1 thread - sync/writing thread
You should either have a single synchronized queue or one queue for
each worker thread (still requires synchronization, but will have less
contention).
You sould have an output data pool. I'm not sure what the best
collection to use but perhaps a dictionary is best. Output data also
needs to be synchronized.
The reader thread can read items and then put them into the queue(s).
They need to be tagged with their sort order, ideally sequential
numbers starting with zero.
Worker threads all continuously take data out of the queue(s), perform
the calculations, a put results into the output pool.
The writer thread has to take stuff out of the pool but also make sure
it does so in the right order. If you have a single sequential
number, then you can just do a simple loop and block when the next
needed value is not ready.
The important performance issue is the synchronization points. You
want to limit the amount of time threads are waiting on each other.
You can do this by having individual threads read/write a few values
each time they get a lock or by separating collections so there are
less objects interacting with each collection (although there will
always be at least two.. the reader or writer, as well as at least one
worker thread).
Once you have a working model, you can tweak the number of worker
threads to find the ideal number for your situation (can even increase
the number dynamically based on load).
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 3 Aug 2007 09:26:01 -0700, reeset
<reeset@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I'm hoping someone can help me understand why running a set of processes
through the threadpool seems to be slower than running the same process
through a single thread (maybe I'm minunderstanding what the threadpool class
is good for). Let me example what I need to do:
- Follow-Ups:
- Re: ThreadPool Questions
- From: Peter Duniho
- Re: ThreadPool Questions
- From: Rick Lones
- Re: ThreadPool Questions
- References:
- ThreadPool Questions
- From: reeset
- Re: ThreadPool Questions
- From: Samuel R . Neff
- ThreadPool Questions
- Prev by Date: Re: Dynamically Loading Assembly and Accessing its Types (namespaces are different)
- Next by Date: Re: Event raised when Application/Form regains focus...
- Previous by thread: Re: ThreadPool Questions
- Next by thread: Re: ThreadPool Questions
- Index(es):
Relevant Pages
|