7 3 7 併發多執行緒 死鎖和遞迴鎖

2022-05-17 04:07:13 字數 3399 閱讀 9312

所謂死鎖: 是指兩個或兩個以上的程序或執行緒在執行過程中,因爭奪資源而造成的一種互相等待的現象,若無外力作用,它們都將無法推進下去。此時稱系統處於死鎖狀態或系統產生了死鎖,這些永遠在互相等待的程序稱為死鎖程序,如下就是死鎖

from threading import

thread,lock

import

time

mutexa=lock()

mutexb=lock()

class

mythread(thread):

defrun(self):

self.func1()

self.func2()

deffunc1(self):

mutexa.acquire()

print('

\033[41m%s 拿到a鎖\033[0m

' %self.name)

mutexb.acquire()

print('

\033[42m%s 拿到b鎖\033[0m

' %self.name)

mutexb.release()

mutexa.release()

deffunc2(self):

mutexb.acquire()

print('

\033[43m%s 拿到b鎖\033[0m

' %self.name)

time.sleep(2)

mutexa.acquire()

print('

\033[44m%s 拿到a鎖\033[0m

' %self.name)

mutexa.release()

mutexb.release()

if__name__ == '

__main__':

for i in range(10):

t=mythread()

t.start()

死鎖

thread-1拿到a鎖

thread-1拿到b鎖

thread-1拿到b鎖

thread-2 拿到a鎖 #

出現死鎖,整個程式阻塞住

執行結果

以上的原因是mutexa,mutexb只能鎖(acquire)一次,釋放(release)一次,下次才能繼續鎖,也就是不能鎖了以後在鎖。

解決以上的辦法是可以用遞迴鎖,也就是一把鎖,可以鎖n次,每次鎖一次,該鎖的計數器就加1,鎖一次加1次,釋放一次,鎖計數器減一次,直到該遞迴鎖的計數器為0時候才能被別人搶走。

解決方法,遞迴鎖,在python中為了支援在同一執行緒中多次請求同一資源,python提供了可重入鎖rlock。

這個rlock內部維護著乙個lock和乙個counter變數,counter記錄了acquire的次數,從而使得資源可以被多次require。直到乙個執行緒所有的acquire都被release,其他的執行緒才能獲得資源。上面的例子如果使用rlock代替lock,則不會發生死鎖,二者的區別是:遞迴鎖可以連續acquire多次,而互斥鎖只能acquire一次

from threading import

thread,rlock

import

time

mutexa=mutexb=rlock() #

乙個執行緒拿到鎖,counter加1,該執行緒內又碰到加鎖的情況,則counter繼續加1,這期間所有其他執行緒都只能等待,等待該執行緒釋放所有鎖,即counter遞減到0為止

class

mythread(thread):

defrun(self):

self.func1()

self.func2()

deffunc1(self):

mutexa.acquire()

print('

\033[41m%s 拿到a鎖\033[0m

' %self.name)

mutexb.acquire()

print('

\033[42m%s 拿到b鎖\033[0m

' %self.name)

mutexb.release()

mutexa.release()

deffunc2(self):

mutexb.acquire()

print('

\033[43m%s 拿到b鎖\033[0m

' %self.name)

time.sleep(2)

mutexa.acquire()

print('

\033[44m%s 拿到a鎖\033[0m

' %self.name)

mutexa.release()

mutexb.release()

if__name__ == '

__main__':

for i in range(10):

t=mythread()

t.start()

遞迴鎖

thread-1拿到a鎖

thread-1拿到b鎖

thread-1拿到b鎖

thread-1拿到a鎖

thread-2拿到a鎖

thread-2拿到b鎖

thread-2拿到b鎖

thread-2拿到a鎖

thread-4拿到a鎖

thread-4拿到b鎖

thread-4拿到b鎖

thread-4拿到a鎖

thread-6拿到a鎖

thread-6拿到b鎖

thread-6拿到b鎖

thread-6拿到a鎖

thread-8拿到a鎖

thread-8拿到b鎖

thread-9拿到a鎖

thread-9拿到b鎖

thread-9拿到b鎖

thread-9拿到a鎖

thread-3拿到a鎖

thread-3拿到b鎖

thread-3拿到b鎖

thread-3拿到a鎖

thread-7拿到a鎖

thread-7拿到b鎖

thread-7拿到b鎖

thread-7拿到a鎖

thread-10拿到a鎖

thread-10拿到b鎖

thread-10拿到b鎖

thread-10拿到a鎖

thread-8拿到b鎖

thread-8拿到a鎖

thread-5拿到a鎖

thread-5拿到b鎖

thread-5拿到b鎖

thread-5 拿到a鎖

執行結果

執行緒死鎖和遞迴鎖

import threading import time class mythread threading.thread def a self locka.acquire 獲取鎖 print self.name,getlocka time.ctime time.sleep 3 lockb.acqui...

併發程式設計(十二) 死鎖和遞迴鎖

死鎖是一種現象 兩個及以上的程序或者執行緒在爭搶資源的過程中,出現的互相等待的現象 如果沒有外部干預,他們就一直僵持,永遠在互相等待,就 死 住了 看一下現象 from threading import thread,lock def func a,b a.acquire print a 這是fun...

併發程式設計(十二) 死鎖和遞迴鎖

死鎖是一種現象 兩個及以上的程序或者執行緒在爭搶資源的過程中,出現的互相等待的現象 如果沒有外部干預,他們就一直僵持,永遠在互相等待,就 死 住了 看一下現象 from threading import thread,lock def func a,b a.acquire print a 這是fun...