Connecting Tech Pros Worldwide Help | Site Map

Threading .Join method

Newbie
 
Join Date: Jun 2009
Location: Texas
Posts: 4
#1: Jun 23 '09
I've been finding different and confusing descriptions of the .join method pertaining to threads online, I've tried it in a few tests and this is what I'm gathering...

I am under the impression, contrary to some websites I have visited, that the .join method blocks any other threads from starting until the calling thread is finished. Also, if another thread has already started before the .join is called, it is not affected and continues on as normal. Is that correct?

This is probably an easy yes, but I just want to be sure as if that's the case I'll be using it a LOT in my upcoming project.

Thanks!
Newbie
 
Join Date: Jun 2009
Posts: 1
#2: Jun 24 '09

re: Threading .Join method


Join method only affects the calling thread, no other threads are affected. Therefore it blocks the calling thread only, as it is said in documentation.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Jun 24 '09

re: Threading .Join method


Quote:

Originally Posted by ocryan View Post

...

This is probably an easy yes, but I just want to be sure as if that's the case I'll be using it a LOT in my upcoming project.

Thanks!

That is a worrying statement.If you find yourself using a lot of threads but blocking them a lot (e.g. using join) then perhaps you don't need threads at all. Most of your tasks will run one after the other.
Just trying to make sure you have your design right.
Expert
 
Join Date: Jan 2008
Location: York
Posts: 179
#4: Jun 24 '09

re: Threading .Join method


Quote:

Originally Posted by ocryan View Post

I've been finding different and confusing descriptions of the .join method pertaining to threads online, I've tried it in a few tests and this is what I'm gathering...

I am under the impression, contrary to some websites I have visited, that the .join method blocks any other threads from starting until the calling thread is finished. Also, if another thread has already started before the .join is called, it is not affected and continues on as normal. Is that correct?

This is probably an easy yes, but I just want to be sure as if that's the case I'll be using it a LOT in my upcoming project.

Thanks!

As pingw33n said, that isn't correct. The join method blocks the calling thread until the thread in question either stops or the timeout expires. All other threads will continue execution and new threads can be started from other threads of execution.
Newbie
 
Join Date: Jun 2009
Location: Texas
Posts: 4
#5: Jun 24 '09

re: Threading .Join method


Quote:

Originally Posted by IanWright View Post

As pingw33n said, that isn't correct. The join method blocks the calling thread until the thread in question either stops or the timeout expires. All other threads will continue execution and new threads can be started from other threads of execution.

Hmm, I did some testing with the .join and it doesn't seem to block the calling thread, I believe all of you of course, but this is the code I used, if you could tell me where and how I am misinterpreting this, I'd greatly appreciate it.

For example, when I do this...
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.         {
  3.             Thread t1 = new Thread(methodName);
  4.             t1.Name = "t1";
  5.             t1.Start();
  6.  
  7.             Thread t2 = new Thread(methodName);
  8.             t2.Name = "t2";
  9.             t2.Start();
  10.  
  11.             t2.Join();
  12.  
  13.             Thread t3 = new Thread(methodName);
  14.             t3.Name = "t3";
  15.             t3.Start();
  16.         }
  17.  
  18.         static void methodName()
  19.         {
  20.             for (int x = 0; x < 10; x++)
  21.             {
  22.                 Thread.Sleep(1000);
  23.                 Console.WriteLine(Thread.CurrentThread.Name + " at " + x.ToString());
  24.             }
  25.         }
  26.  

Both t1 and t2 continue running, and when t2 is finished, t3 starts. That is why I made my previous statement about what I thought .join was doing.
Newbie
 
Join Date: Jun 2009
Location: Texas
Posts: 4
#6: Jun 24 '09

re: Threading .Join method


Quote:

Originally Posted by r035198x View Post

That is a worrying statement.If you find yourself using a lot of threads but blocking them a lot (e.g. using join) then perhaps you don't need threads at all. Most of your tasks will run one after the other.
Just trying to make sure you have your design right.

Unfortunately I need threads because I'm dealing with simulating execution paths that are in parallel, and need to be run in parallel to be sufficiently tested. I wish it weren't the case. :/
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#7: Jun 25 '09

re: Threading .Join method


I don't see how your results lead you to your conclusion. You are calling join on the Main program thread and so it's the Main program thread that gets blocked until t2 is finished. That is why t3 only starts after t2 has finished. Exactly like has been explained above.
Quote:
Unfortunately I need threads because I'm dealing with simulating execution paths that are in parallel, and need to be run in parallel to be sufficiently tested. I wish it weren't the case. :/
Read that post of mine again. I did not say that you don't need threads. I said that if you do need threads then calling join a lot like you said you are planning to do smells of a bad design. That would make your tasks run sequentially instead of in parallel and thus defeats the purpose of using threads in the first place.
Expert
 
Join Date: Jan 2008
Location: York
Posts: 179
#8: Jun 25 '09

re: Threading .Join method


r035198x pointed this out,

You actually have 3 threads.
Main
t1
t2

Main is the calling thread for t1 and t2.
Newbie
 
Join Date: Jun 2009
Location: Texas
Posts: 4
#9: Jun 25 '09

re: Threading .Join method


Ah, that makes more sense. I was making an extremely bad assumption. I'm new when it comes to using threading and am trying to catch up, thanks for the help!
Reply


Similar C# / C Sharp bytes