递归锁(RLock)
递归锁(RLock)
-
递归锁可以开启多个锁(开启几个就要关闭几个),经常用于解决死锁现象
- 将多个锁,改成同时使用一把递归锁
-
缺点:效率没有互斥锁高
-
代码
from threading import Thread,RLock a=0 def son1(rlock): with rlock: for i in range(100): global a a+=1 def son2(rlock): with rlock: global a for i in range(100): a-=1 rlock=RLock() ret=Thread(target=son1,args=(rlock,)).start() ret1=Thread(target=son2,args=(rlock,)).start() print(a)