from threading import Thread,Lock
import os,time
def work(lock):
global n
with lock:
temp=n
time.sleep(0.1)
n=temp-1
n=temp-1
if __name__ == ‘__main__‘:
n=100
l=[]
lock = Lock()
for i in range(100):
p=Thread(target=work,args = (lock,))
l.append(p)
p.start()
for p in l:
p.join()
print(n) #结果可能为99
原文:https://www.cnblogs.com/yuanshitianzun/p/10473282.html