So I have this code:
import time
import threading
bar = False
def foo():
while True:
if bar == True:
print "Success!"
else:
print "Not yet!"
time.sleep(1)
def example():
while True:
time.sleep(5)
bar = True
t1 = threading.Thread(target=foo)
t1.start()
t2 = threading.Thread(target=example)
t2.start()
I'm trying to understand why I can't get bar to = to true.. If so, then the other thread should see the change and write Success!
barin the two functions are not in the same scope. You should deal with scopes before you learn multithreading. In any case there should be mutual-resource constructs you can use for threads.time.sleep(1)is wrong. I think is was intended to be inside the while loop.