互斥锁
import time
tick = 100
def func1():
global tick
for i in range(100):
lock.acquire()
tick +=5 #外部传进来,变成自己的局部变量,与外部无关
time.sleep(0.01)
tick -=5
lock.release()
print(tick)
def func2():
global tick
for i in range(10):
lock.acquire()
tick +=10
time.sleep(0.05)
tick -=10
lock.release()
print(tick)
if __name__=='__main__':
lock = threading.Lock()
t1=threading.Thread(target=func1)
t2=threading.Thread(target=func2)
t2.setDaemon(True)
t1.setDaemon(True)
t1.start()
t2.start()
#t1.join()
#t2.join()```

浙公网安备 33010602011771号