You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
386 B

import threading
num = 0
lock = threading.Lock()
def add():
global num
lock.acquire()
num += 1
lock.release()
def sub():
global num
lock.acquire()
num -= 1
lock.release()
for i in range(1000):
t1 = threading.Thread(target=add, )
t2 = threading.Thread(target=sub, )
t1.start()
t2.start()
t1.join()
t2.join()
print(num)