Thread: Thread join() question

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Thread join() question

    "Blocks the current thread until the thread identified by *this finishes its execution. "

    join() appears similar to function in Java but the description given in cppreference.com is a bit terse and not very explanatory.

    Does this mean that the current thread, for instance being the only one started in main,

    Code:
    thread run(foo);
    will block until int main returns?

    Can someone please elaborate?
    Last edited by Imanuel; 01-27-2013 at 07:03 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    All it means is that the thread which executed the join command waits for the thread that it was told to join, to end.
    Really it should be called "waitForCompletion".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by iMalc View Post
    All it means is that the thread which executed the join command waits for the thread that it was told to join, to end.
    Really it should be called "waitForCompletion".
    This is not making any sense to me. I start two threads and when they are done, they call join. But what are they joining to? Who or what are they waiting for? Themselves to quit? The function is not worded well or given a good explanation at cppreference.com.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the threads themselves don't call join.

    this is one possible flow of execution:

    main starts 2 threads.
    thread 1 starts
    thread 2 starts
    main calls join() on thread 1
    thread 1 finishes
    main calls join() on thread 2
    thread 2 finishes (may happen earlier, but this is for the sake of simplicity)
    program ends

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-20-2011, 12:01 AM
  2. IPC thread question
    By Izzy123 in forum C Programming
    Replies: 2
    Last Post: 06-17-2011, 09:09 AM
  3. Question on thread
    By flexo87 in forum C Programming
    Replies: 10
    Last Post: 01-28-2009, 10:52 AM
  4. question about thread
    By wgan in forum Windows Programming
    Replies: 6
    Last Post: 06-30-2003, 12:36 PM
  5. General Thread question
    By Magos in forum Windows Programming
    Replies: 3
    Last Post: 09-13-2002, 12:49 PM