在python的多線程中,使用threading中的lock rlock鎖, 為何rlock可以調用多次acquire,lock缺不能,lock調用多次而且會發(fā)生死鎖,rlock不會,求大神指點下
閉關修行中......
rlock is a reentrant lock. You can simply understand that it comes with a counter. Acquire has a counter of +1, and release has a counter of -1. Negative values ??are not allowed, otherwise an exception will occur.
Why do we do this? Because the application scenarios are different, a reentrant lock can call another method that requires the lock, but a non-reentrant lock cannot do this.
def fun1():
rlock.acquire()
fun2()
rlock.release()
def fun2():
rlock.acquire()
rlock.release()
The difference between lock and rlock is r:
reentrant, can be entered repeatedly. A thread can acquire the same rlock multiple times without being blocked. If a thread acquires rlock multiple times, it must release the same number of times before it can be released. rlock.
Lock is different. It can only be acquired once and cannot be acquired again before it is released.
For more information, please refer to this answer:
http://stackoverflow.com/ques...