Thread problem ( Pool)

R

Ricardo Luceac

HI...

I have a queue of objects waiting to be analysed by a method, so I think
o using threads to analyse 3 objects at the same time...

The problem is:

I create the 3 threads, and execute them, the threads look in the queue,
pick an object and analyse it. But after the thread analyse the object,
it finishs...

How can I do that after the thread is finished, if there's any item in
the queue, it start the thread again??

thx...
 
S

Steve Walker

Ricardo Luceac said:
HI...

I have a queue of objects waiting to be analysed by a method, so I think
o using threads to analyse 3 objects at the same time...

The problem is:

I create the 3 threads, and execute them, the threads look in the queue,
pick an object and analyse it. But after the thread analyse the object,
it finishs...

How can I do that after the thread is finished, if there's any item in
the queue, it start the thread again??

Might be worth you having another look at the design. On the face of it,
it doesn't sound to me as if running extra threads will make your
operation run any faster. If you decide to go ahead with your design, I
would have thought that the code you launch within the threads should
run in a loop, dequeuing and analysing objects until there are none
left, and then terminate. It will terminate at the exit point of the
method you used in the threadstart delegate.

Just to reiterate, what advantage do you think you will get by
multithreading this?
 
J

Jon Skeet [C# MVP]

Ricardo Luceac said:
I have a queue of objects waiting to be analysed by a method, so I think
o using threads to analyse 3 objects at the same time...

The problem is:

I create the 3 threads, and execute them, the threads look in the queue,
pick an object and analyse it. But after the thread analyse the object,
it finishs...

How can I do that after the thread is finished, if there's any item in
the queue, it start the thread again??

It sounds like you're after a producer/consumer queue in combination
with a thread-pool.

See http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml
(half way down) for a sample producer-consumer queue, and
http://www.pobox.com/~skeet/csharp/miscutil for a custom thread pool -
I try not to use the system one myself, as it's annoyingly easy to
deadlock it.
 

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