#多个线程操作同一个数据的时候,就得加锁
import threading
num = 0
lock = threading.Lock()#申请一把锁
def add():
global num
lock.acquire()#加锁
num+=1
lock.release()#解锁
for i in range(20):
t = threading.Thread(target=add,)
t.start()

while threading.activeCount()!=1:
pass
print(num)
posted @ 2019-11-19 23:05  一只竹子  阅读(131)  评论(0编辑  收藏  举报