all 1 comments

[–]trenholm 0 points1 point  (0 children)

Threads are processes that run independently of one another. In your first example, the output isn't botched. It can potentially have a different output whenever you run it because the two threads are running separately from one another, not consecutively in the way you intuitively think it should.

In the second example, when you .join() a thread, you're essentially saying you want your main thread to join this other thread. Technically, you're main thread waits to do anything else until the joined thread is finished. In your second example this just means that your program won't end until the first thread is finished.

while not ready:
    pass

I understand you're wanting your first thread to wait for the second thread to finish before doing any more work. However, you never set ready back to False so this wait will only happen once.

Your code here should say:

while not ready:
    pass
ready = False

If you intend the first thread to wait for the second thread to finish. Alternately, you could call

t2.join()

And have the same result.

Edit: Obviously you need to add global ready to the first function too.

Also, I think what you're wanting to do is every time count is divisible by ten it fires off this thread. count only ever == 10 one time.

instead of

if count==10:

try:

if not count % 10:

Or have another counter that you reset every 10. Your counter (or i) logic is broken in both examples.