1

This is a rather silly question...yet it bugs the heck out of me. In the threading module we have a Thread.join() method. I understand that it blocks the calling thread, but why is .join() called 'join'? Why is it not called main_thread_wait or block_parent_thread or something like that?

Every time I see .join(), I feel the child thread is joining something to get caclulations done. Is there some reason why this particular keyword was chosen for a method name?

martineau
  • 119,623
  • 25
  • 170
  • 301
JavaFan
  • 1,295
  • 3
  • 19
  • 28
  • 1
    https://stackoverflow.com/questions/25345418/the-join-function-in-threading . First answer. And a visual https://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading – Pythonista Jun 08 '18 at 17:09
  • The name is copied from [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html), which I believe copied it from various Unixy threading implementations written before threading was standardized. – Kevin Jun 08 '18 at 17:12

2 Answers2

1

The child-thread joins the parent. Thread.start makes the parent give birth to a thread and let it free. Thread.join makes the parent block (wait) until the child is done doing its job.

You can imagine it like this:

  • the parent thread is a mighty god that can live for a long time
  • with Thread.start it gives birth to a mortal
  • right after its birth, the mortal runs away and starts living its own life (executing its run method)
  • the god may continue minding his own business
  • he also may want to take a look at his offspring, to see whether, literally, it is_alive
  • he also may start observing the whole life of the offspring, waiting for him to come back, right until the death of the latter (this is the join method). While doing that, the god cannot do anything else, because he enjoys admiring his creature so much (the call to join blocks the parent thread), but after the mortal dies, the god can continue his life
ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

This name is far older than Python. It appears in the POSIX threading library (libpthread) as pthread_join() (the "pthread" prefix is used because C has no namespaces). However, for process level concurrency, both Python and POSIX use the name wait().

Other parts of the pthread library do use the term wait, but usually for higher-level synchronization primitives, such as pthread_barrier_wait() or pthread_cond_wait(). I imagine that pthread_wait() would have been ambiguous between "wait for [some synchronization object]" and "wait for a thread to terminate."

Kevin
  • 28,963
  • 9
  • 62
  • 81