I was expecting a race condition to occur here. But it doesn't. Why?
import threading
x = 0
def foo():
global x
for i in range(1000):
print x
x += 1
def bar():
global x
for i in range(1000):
print x
x -= 1
if __name__ == "__main__":
t1 = threading.Thread(target=foo)
t2 = threading.Thread(target=bar)
t1.start(); t2.start()
t1.join(); t2.join()
print x
The final value of x always 0. Not only that it seems that the second thread only starts to run after thread 1 has completed it's execution.