import threading
x = 0
def up(count):
global x
for _ in range(count):
x += 1
def down(count):
global x
for _ in range(count):
x -= 1
iterations = 10
t1 = threading.Thread(target=up, args=(iterations, ))
t2 = threading.Thread(target=down, args=(iterations, ))
t1.start()
t2.start()
t1.join()
t2.join()
print(x)
We use a global variable x
in both the up and the down function.
The result you are expecting and most probably will get is 0