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,
will block until int main returns?
Can someone please elaborate?
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.
Originally Posted by iMalc
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)