day47- thread - lock and deadlock

# 1, lock: prevent multiple threads simultaneously read and write a block of memory. 
from Threading Import the Thread
 from Threading Import Lock
 DEF FUNC ():
     Global n- 
    lock.acquire () 
    n- -. 1 =         # of each thread before to get the key to the operation data, the operation is completed, the release key. 
    lock.release () 

the n- = 10 
t_list = [] 
Lock = Lock ()
 for i in the Range (10):          # First open all child threads. 
    the Thread = T (target = FUNC) 
    t.start () 
    t_list.append (T) 
[t.join () fort in t_list] # finally let the main thread wait for the end of all sub-thread ends. So as to achieve an asynchronous operation. 
Print (n-) 

# 2, RLOCK can acquire multiple recursive lock and correspondingly release times, Lock can acquire and release the mutex once. 
from Threading Import RLOCK 
Lock = RLOCK () 
lock.acquire () 
lock.acquire () 
lock.acquire () 
Print ( ' 123 ' ) 
lock.release () 
lock.release () 
lock.release () 
# 123 

# . 3, deadlock: in different threads among (eat and eat1), just to be on the two data (chopsticks and face) operation, Lock will produce a deadlock, data insecurity. 
# Scientist noodles: noodles and chopsticks need to face in order to eat, A surface finish down the chopsticks and noodles give B, B to get the chopsticks and noodles to eat. 
#            The results of the following example is to get people to get the surface of chopsticks, chopsticks get get surface was formed deadlock. 
from Threading Import Lock
 from Threading Import the Thread
 Import Time
 DEF EAT (name): 
    kz_lock.acquire () 
    Print ( ' % S got chopsticks ' % name) 
    m_lock.acquire () 
    Print ( ' % S to get the surface of ' % name)
     Print ( ' % S can eat face a ' % name) 
    the time.sleep ( 1) # because the cpu scheduling is disordered, set the sleep one second is to explore the problem better, otherwise even perform many times, problem is a small probability event.
    m_lock.release () 
    kz_lock.release () 

DEF eat1 (name): 
    m_lock.acquire () 
    Print ( ' % S got a face " % name) 
    kz_lock.acquire () 
    Print ( ' % S got chopsticks ' % name)
     Print ( ' % S noodles can be a ' % name) 
    kz_lock.release () 
    m_lock.release () 

kz_lock = lock () # chopsticks lock 
m_lock = lock ()   # plane locks 
t = Thread (target = eat, args = ( ' Tom ' ,))  
t.start ()
T1The Thread = (target = eat1, args = ( ' Marry ' ,)) 
t1.start () 
T2 = the Thread (target = EAT, args = ( ' Jack ' ,)) 
t2.start () 
T3 = the Thread (target = eat1 , args = ( ' Alex ' ,)) 
t3.start () 
# Tom got chopsticks 
# Tom got a plane 
# Tom can eat the surface 
# Jack get chopsticks 
# Marry the surface got 

# R lock deadlocks problem: 
from Threading Import RLOCK
 from Threading Import the Thread
 Import Time
DEF name)EAT (name): 
    kz_lock.acquire () 
    Print ( ' % S got chopsticks ' % name) 
    m_lock.acquire () 
    Print ( ' % S to get the surface ' % name)
     Print ( ' % S can eat the surface ' % name) 
    the time.sleep ( . 1 ) 
    m_lock.release () # get back to the key further. 
    kz_lock.release () # also get the key to the front. 

DEF eat1 (name): 
    m_lock.acquire () 
    Print ( ' % S to get the surface ' % 
    kz_lock.acquire () 
    Print ( ' % S got chopsticks ' % name)
     Print ( ' % S noodles can be a ' % name) 
    kz_lock.release () 
    m_lock.release () 

kz_lock = m_lock = RLOCK () # chopsticks lock and locking surface are the same lock 
T = the Thread (target = EAT, args = ( ' Tom ' ,)) 
t.start () 
T1 = the Thread (target = eat1, args = ( ' Marry ' ,)) 
t1.start ( ) 
T2 = the Thread (target = EAT, args = ( ' Jack ' ,)) 
t2.start () 
T3The Thread = (target = eat1, args = ( ' Alex ' ,)) 
t3.start () 
# Tom got chopsticks 
# Tom got a plane 
# Tom can eat the surface 
# Marry the surface got 
# Marry get chopsticks the 
# Marry eat face a 
# Jack got chopsticks 
# Jack got a face 
# Jack can eat a face 
# alex got a face 
# alex got chopsticks 
# alex can eat noodles a 


# an R lock to solve the deadlock problem ( more concise wording): 
from Threading Import RLOCK
 from Threading Import the Thread
 DEF EAT (name): 
    lock.acquire ()
    Print ( '% s got chopsticks ' % name) 
    lock.acquire () 
    Print ( ' % s surface to get a ' % name)
     Print ( ' % s may be the noodles ' % name) 
    lock.release () 
    lock.release ( ) 

DEF eat1 (name): 
    lock.acquire () 
    Print ( ' % S to get the surface ' % name) 
    lock.acquire () 
    Print ( ' % S got chopsticks ' % name)
     Print ( ' % S can eat surface of ' % name)
    lock.release()
    lock.release()

lock = RLock()
t = Thread(target=eat,args=('tom',))
t.start()
t1 = Thread(target=eat1,args=('marry',))
t1.start()
t2 = Thread(target=eat,args=('jack',))
t2.start()
t3 = Thread(target=eat1,args=('alex',))
t3.start()

Guess you like

Origin www.cnblogs.com/python-daxiong/p/12142779.html