标签:死锁 for 寄存器 class div __name__ time fun release
互斥锁只能一次acquire 然后releae ,不能进行多次acquire容易出现死锁,递归锁就可以多次acquire
递归锁:递归锁可以多次进行acquire,每进行一次acquire,寄存器的基数就加1,一直等到基数变成0的时候才能被其他线程拿到
from threading import Thread,RLock import time class MyThread(Thread): def run(self): self.func1() self.func2() def func1(self): mutexA.acquire() # +1 print(‘%s 拿到A锁‘ %self.name) mutexB.acquire() # +1=2 print(‘%s 拿到B锁‘ %self.name) mutexB.release() # -1=1 mutexA.release() # -1=0 def func2(self): mutexB.acquire() # +1 print(‘%s 拿到B锁‘ %self.name) time.sleep(2) # 休息 mutexA.acquire() print(‘%s 拿到A锁‘ %self.name) mutexA.release() mutexB.release() if __name__ == ‘__main__‘: mutexB = mutexA = RLock() for i in range(10): t=MyThread() t.start()
标签:死锁 for 寄存器 class div __name__ time fun release
原文地址:https://www.cnblogs.com/whileke/p/11465068.html