# __author: "ZXYang"
# date: 2020/5/12
import threading
import time
# from time import time
def addnum():
global num
lock.acquire() # 加锁,计算完后,进行下个线程,比t.join效果更好 ==> 先占着cpu
# print('ok')
# time.sleep(10) # 时间太长,下一个线程会覆盖,数据被覆盖(在 -1 之前)
# time.sleep(0.0001)
num -= 1
lock.release() # 释放
num = 100
thread_list = []
# lock = threading.Lock()
# lock = threading.Lock()
lock = threading.RLock() # RLock一定要加()
for i in range(100):
t = threading.Thread(target=addnum)
t.start()
thread_list.append(t)
for t in thread_list:
# t.join()
print('finally num', num)