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.
30 lines
391 B
30 lines
391 B
import threading
|
|
|
|
|
|
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):
|
|
num = 0
|
|
t1 = threading.Thread(target=add, )
|
|
t2 = threading.Thread(target=sub, )
|
|
t1.start()
|
|
t2.start()
|
|
t1.join()
|
|
t2.join()
|
|
print(num)
|
|
|
|
|
|
|