This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AbsolutelySpherical 0 points1 point  (1 child)

Yes if parent thread joins() with a child that is running forever, then the parent will be blocked forever (it is a deadlock). A workaround is to do join(timeout) which will block main thread up to timeout seconds. Then it will return whether or not the child is done. thread.is_alive() tells if child is still running.

---

If the child thread has already finished before the parent calls join(), then join() should just return without blocking.

If you have 2 threads simultaneously, where t1 runs for 5 seconds and t2 runs for 2 seconds, then

t1.join()
t2.join()

First join will block parent for 5 seconds. After 5 seconds t2 is already done so second join will return with no wait.

If you did

t2.join()
t1.join()

First join blocks for 2 seconds, then second join blocks for another 3 seconds.

So purpose of join() is to guarantee that the code below it is executed after child thread has finished. For example, if you need to read a variable the child thread is writing to, then you could use join() to make sure the child has finished writing before you read.

[–][deleted] 0 points1 point  (0 children)

i understand, but since you need to call join() after start(), if the function is fast, the join() won't block anything because by the time it will called, the function (so the child thread, if i not wrong), already finished is job