views:

90

answers:

4

I'm creating three threads in one of my application. The requirement I have is that the method that creates these three threads and starts them off should not return unless all these three threads are executed.

I tried to use Join on all three threads. However, I observe that when I use Join the total execution time of my method is the sum of execution times of all three threads. In other words they are getting executed in sequence.

I tried using ThreadState but realized from MSDN and stackoverflow that ThreadState property should only be used for debugging purposes and not for real coding.

What is the best way I can achieve this and keep the execution parallel.

Any ideas will be much appreciated. Thanks in advance

+4  A: 

However, I observe that when I use Join the total execution time of my method is the sum of execution times of all three threads. In other words they are getting executed in sequence.

That suggests you're not starting the threads properly, or they're performing locking which is preventing them from executing in parallel. Calling Join on each thread in turn is a simple and effective way of waiting for each thread to finish.

One bug I can imagine is if you're starting each thread and calling Join as soon as you've started it rather than waiting until you've started all the threads. For example:

// Incorrect code
for (int i = 0; i < 3; i++)
{
     Thread t = new Thread(Task);
     t.Start();
     t.Join();
}

Instead, you should start all three threads and then join all of them:

List<Thread> threads = new List<Thread>();
for (int i = 0; i < 3; i++)
{
    Thread t = new Thread(Task);
    t.Start();
    threads.Add(t);
}
foreach (Thread t in threads)
{
    t.Join();
}

Does that explain your issue? If not, please post code showing what you're doing.

Jon Skeet
Jon, this was exactly the problem. Thanks for "guessing" the problem. I resolved it moving all join statements after all threads have been started
KPK
@KPK: if it worked, accept Jon's answer by clicking the green checkbox left of it: http://stackoverflow.com/faq. PS: welcome at SO!
Abel
+1  A: 

I suspect your problems is with the way in wich you are creating and running the threads, as Thread.Join will not cause your threads to run serially (one after the other) if used correctly. What it will do is make you wait until the longest running thread has completed.

Can you post some samples of your code?

Rob Levine
A: 

You could do this asynchronously with delegates.

In the call back handler implement some logic that sets a flag when one of the threads is complete.

In your dispatcher, you then just wait will this flag is set for all threads then return.

But in honesty, If you need to do this, you might be approaching the problem from the wrong angle.

As the others have asked, can you post some code, or explain what you hope to achieve ?

Russ C
+1  A: 

Hi,

You could try this:

http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx

I believe that is the best way and recommended way to handle this.

radu
Handle what? We don't have any actual detail of what the code looks like and why this might be happening. Thread.Join probably has nothing to do with the issue the OP is describing.
Rob Levine
Handle his issue.Regarding his code, you are right we don't know how it looks like, but i suppose that he is calling a method in threads, so to wait for all of them to be finished and than return to foreground thread i know that this is the best wait to do it... (when is needed to wait for more than one thread , if you should wait for only one thread join is the recommended way to do it)
radu
No need for a mod down, it's not a bad suggestion based on the sparseness of the op.
Russ C
@Russ C - you are right. I thought better of it after I read radu's comment but couldn't undo the downvote as too much time had passed. However, just realised I can edit the question (removed some whitespace!) and that unlocks it. Downvote removed.
Rob Levine
It's all good :)
Russ C
Thank you guys for your interest ;)
radu