KPK
KPK

Reputation: 53

The execution time of my threaded code does not benefit from threads with Join

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

Upvotes: 1

Views: 166

Answers (4)

radu
radu

Reputation: 11

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.

Upvotes: 1

Russ Clarke
Russ Clarke

Reputation: 17909

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 ?

Upvotes: 0

Rob Levine
Rob Levine

Reputation: 41328

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?

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502536

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.

Upvotes: 5

Related Questions