Python Forum
Threads join() question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Threads join() question (/thread-16906.html)



Threads join() question - adam2020 - Mar-19-2019

Hi guys,

so I'm pretty new to threading, I have a question in regards to the join function let me put some example code below

import threading

def funcOne():
     print(threading.current_thread().getName())

def funcTwo():
    print(threading.current_thread().getName())


t1 = threading.Thread(target = funcOne, name = "T one")
t2 = threading.Thread(target = funcTwo, name = "T two")

t1.start()
t2.start()

t1.join()
t2.join()

print("bye")
so my question is, I start both threads with t1.start() and t2.start(), so what does the join function do here? from what I have took in, the join() function will make sure that thread finishes before running anymore threads/ functions, BUT as you can see I call t1.join() so now does main stop functioning until t1 finishes? if so is t2 still running?? does the t2.join() function get called after the thread t1 finishes?

thanks


RE: Threads join() question - nilamo - Mar-19-2019

Thread.join() will stop the current thread, until the referenced thread has completed. In your example, t1.join() would stop the main thread, while both t1 and t2 continue running. Once t1 completes, the main thread would resume. The very next line, t2.join(), would again halt the main thread until t2 had completed. At that point, you had already guaranteed that t1 had completed, so the only running thread would be t2.

Once t2 completes, the main thread would resume. At the next instruction, print("bye"), the main thread is the only thread still running.


RE: Threads join() question - adam2020 - Mar-19-2019

(Mar-19-2019, 07:59 PM)nilamo Wrote: Thread.join() will stop the current thread, until the referenced thread has completed. In your example, t1.join() would stop the main thread, while both t1 and t2 continue running. Once t1 completes, the main thread would resume. The very next line, t2.join(), would again halt the main thread until t2 had completed. At that point, you had already guaranteed that t1 had completed, so the only running thread would be t2.

Once t2 completes, the main thread would resume. At the next instruction, print("bye"), the main thread is the only thread still running.

thanks nilamo,

that makes sense, but lets suppose t2 somehow finished before t1, t1.join() finishes now the next line will be t2.join() but t2 has already finished running also, what would happen?

thanks


RE: Threads join() question - nilamo - Mar-19-2019

Then t2.join() would immediately return. Calling .join() will halt the current thread, *if needed*. If that thread is already complete, then it's a do-nothing operation.


RE: Threads join() question - adam2020 - Mar-19-2019

thanks nilamo that makes sense :)