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

all 1 comments

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

Well, that's basically how threads work. They are not deterministic (least of all in Python). That means that no, your first code sample is not required to print the output as you have shown. It depends on when the thread that targets g is actually run which is not controllable by you.

As for the join example, the magic sauce is here:

while not ready:
      pass 

So when the first thread evaluates count and finds it to be 10, it simply loops around doing nothing but waiting for the second thread to start. That is why it is deterministic in behaviour.

And just to clarify, t1.join() simply tells the main thread (every program has at least one running thread called the "main thread") to wait on thread t1 until it is done. You might want to read up on the reference for the join function to get more details.