Threading .Join method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ocryan
    New Member
    • Jun 2009
    • 4

    Threading .Join method

    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!
  • pingw33n
    New Member
    • Jun 2009
    • 1

    #2
    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.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by ocryan
      ...

      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.

      Comment

      • IanWright
        New Member
        • Jan 2008
        • 179

        #4
        Originally posted by ocryan
        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.

        Comment

        • ocryan
          New Member
          • Jun 2009
          • 4

          #5
          Originally posted by IanWright
          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...
          Code:
          static void Main(string[] args)
                  {
                      Thread t1 = new Thread(methodName);
                      t1.Name = "t1";
                      t1.Start();
          
                      Thread t2 = new Thread(methodName);
                      t2.Name = "t2";
                      t2.Start();
          
                      t2.Join();
          
                      Thread t3 = new Thread(methodName);
                      t3.Name = "t3";
                      t3.Start();
                  }
          
                  static void methodName()
                  {
                      for (int x = 0; x < 10; x++)
                      {
                          Thread.Sleep(1000);
                          Console.WriteLine(Thread.CurrentThread.Name + " at " + x.ToString());
                      }
                  }

          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.

          Comment

          • ocryan
            New Member
            • Jun 2009
            • 4

            #6
            Originally posted by r035198x
            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. :/

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              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.
              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.

              Comment

              • IanWright
                New Member
                • Jan 2008
                • 179

                #8
                r035198x pointed this out,

                You actually have 3 threads.
                Main
                t1
                t2

                Main is the calling thread for t1 and t2.

                Comment

                • ocryan
                  New Member
                  • Jun 2009
                  • 4

                  #9
                  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!

                  Comment

                  Working...