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
386 B
30 lines
386 B
![]()
2 years ago
|
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)
|
||
|
|
||
|
|