Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of join() method  RSS feed

 
Rajiv KumarRai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please help me understand what is happening in the above code and how is it happening.
Regarding the join() method, what is joined by what. I am confused by the name of the
method. What makes main thread wait for the child threads. Is it because the the 3 child
threads call join methods inside the main thread. And what changes would be required
if we want to ensure that threads finish in order

I mean like

Child thread One
Child thread Two
Child thread Three
Main thread
 
Henry Wong
author
Sheriff
Posts: 22817
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv KumarRai wrote:
Please help me understand what is happening in the above code and how is it happening.
Regarding the join() method, what is joined by what. I am confused by the name of the
method.
What makes main thread wait for the child threads. Is it because the the 3 child
threads call join methods inside the main thread. And what changes would be required
if we want to ensure that threads finish in order


The join() method causes the currently running thread (the calling thread) to confirm that the thread (represented by the thread object) is not alive. And if it is alive, to wait for it to be no longer alive (ie. to complete). Perhaps the method should have been named something like waitForCompletion() than join(), would that make more sense? And given this answer, does that make it easier to envision the answer to the rest of your questions?

Henry
 
Rajiv KumarRai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What changes can be made to the above code(using join method) so that the threads finish in the following order

Thread One
Thread Two
Thread Three
Main Thread

I guess than I can better understand what join does
 
Henry Wong
author
Sheriff
Posts: 22817
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv KumarRai wrote:What changes can be made to the above code(using join method) so that the threads finish in the following order

Thread One
Thread Two
Thread Three
Main Thread

I guess than I can better understand what join does


First of all, threads that have no other dependencies with each other, other than finishing in a particular order, makes no sense. There have to be some dependency other than finishing in a particular order. For example, thread two needs to print a result which requires thread one to finish first. This means that the threads not only need to finish in a particular order, but the "finishing" needs to be done before certain code can execute in the next thread to finish.

So, to answer your question, with the use of the join() method, it has to be wired to finished that way. Meaning there has to be code in the thread two code path, that calls join() on the thread one object; there has to be code in the thread three code path, that calls join() on the thread two object; etc. etc.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!