The point of this section
One Deadlock
Two Recursive lock
The point of this sectionUnderstand deadlock phenomena and Solutions
The duration of this section needs to be controlled 15 Within minutes
One DeadlockThe so-called deadlock : Refers to two or more processes or threads in the execution process , A phenomenon of waiting for each other caused by competing for resources , If there is no external force , They will not be able to move forward .
At this point, the system is said to be in a deadlock state or the system has produced a deadlock , These processes that are always waiting for each other are called deadlock processes , Here is deadlock
from threading import Thread,Lockimport timemutexA=Lock()mutexB=Lock()class MyThread(Thread): def run(self): self.func1() self.func2() def func1(self): mutexA.acquire() print('\033[41m%s Get A lock \033[0m' %self.name) mutexB.acquire() print('\033[42m%s Get B lock \033[0m' %self.name) mutexB.release() mutexA.release() def func2(self): mutexB.acquire() print('\033[43m%s Get B lock \033[0m' %self.name) time.sleep(2) mutexA.acquire() print('\033[44m%s Get A lock \033[0m' %self.name) mutexA.release() mutexB.release()if __name__ == '__main__': for i in range(10): t=MyThread() t.start()
Execution effect
Thread-1 Get A lock Thread-1 Get B lock Thread-1 Get B lock Thread-2 Get A lock # Deadlock occurred , The whole program is blocked
Two Recursive lock resolvent , Recursive lock , stay Python To support multiple requests for the same resource in the same thread ,python Provides a re-entry lock RLock.
This RLock One is maintained internally Lock And a counter Variable ,counter Recorded acquire The number of times , So that the resource can be multiple times require. All the way to a thread acquire All be release, Only other threads can get the resource .
If the above example uses RLock Instead of Lock, No deadlock , The difference between them is : Recursive locks can be continuous acquire many times , And mutexes can only acquire once
from threading import Thread,RLockimport timemutexA=mutexB=RLock() # A thread gets the lock ,counter Add 1, In this thread, the situation of locking is encountered , be counter Keep adding 1, All other threads can only wait for , Wait for the thread to release all locks , namely counter Descending to 0 until class MyThread(Thread): def run(self): self.func1() self.func2() def func1(self): mutexA.acquire() print('\033[41m%s Get A lock \033[0m' %self.name) mutexB.acquire() print('\033[42m%s Get B lock \033[0m' %self.name) mutexB.release() mutexA.release() def func2(self): mutexB.acquire() print('\033[43m%s Get B lock \033[0m' %self.name) time.sleep(2) mutexA.acquire() print('\033[44m%s Get A lock \033[0m' %self.name) mutexA.release() mutexB.release()if __name__ == '__main__': for i in range(10): t=MyThread() t.start()
That's all python Details of multithread deadlock phenomenon and Solutions , More about python For information on solving multithread deadlock, please pay attention to other relevant articles on software development network !
Make a little progress every d
Recently, in order to improve