一、递归锁
# Lock :互斥锁 效率高
# RLock :递归(recursion)锁 效率相对低 在同一个线程中可以被acquire多次,如果想要释放锁,acquire多少次就要release多少次
from threading import RLock
def func(i,rlock):
rlock .acquire()
rlock .acquire()
print(i,': start')
rlock .release()
rlock .release()
print(i, ': end')
rlock = RLock ()
for i in range(5):
Thread(target=func,args=(i,rlock)).start()
二、死锁
# import time
# from threading import Thread,Lock,RLock
# fork_lock = noodle_lock = RLock()
# # fork_lock = RLock()
#
# def eat(name):
# noodle_lock.acquire()
# print(name,'抢到面了')
# fork_lock.acquire()
# print(name, '抢到叉子了')
# print(name,'吃面')
# time.sleep(0.1)
# fork_lock.release()
# print(name, '放下叉子了')
# noodle_lock.release()
# print(name, '放下面了')
#
# def eat2(name):
# fork_lock.acquire()
# print(name, '抢到叉子了')
# noodle_lock.acquire()
# print(name,'抢到面了')
# print(name,'吃面')
# noodle_lock.release()
# print(name, '放下面了')
# fork_lock.release()
# print(name, '放下叉子了')
#
# Thread(target=eat,args=('alex',)).start()
# Thread(target=eat2,args=('wusir',)).start()
# Thread(target=eat,args=('taibai',)).start()
# Thread(target=eat2,args=('大壮',)).start()
import time
from threading import Thread,Lock,RLock
fork_noodle_lock = Lock()
# fork_lock = RLock()
def eat(name):
fork_noodle_lock.acquire()
print(name,'抢到面了')
print(name, '抢到叉子了')
print(name,'吃面')
time.sleep(0.1)
fork_noodle_lock.release()
print(name, '放下叉子了')
print(name, '放下面了')
def eat2(name):
fork_noodle_lock.acquire()
print(name, '抢到叉子了')
print(name,'抢到面了')
print(name,'吃面')
fork_noodle_lock.release()
print(name, '放下面了')
print(name, '放下叉子了')
Thread(target=eat,args=('a',)).start()
Thread(target=eat2,args=('b',)).start()
Thread(target=eat,args=('c',)).start()
Thread(target=eat2,args=('d',)).start()
#
# 1:死锁现象是怎么产生的?
# 多把(互斥/递归)锁 并且在多个线程中 交叉使用
# fork_lock.acquire()
# noodle_lock.acquire()
#
# fork_lock.release()
# noodle_lock.release()
# 2:如果是互斥锁,出现了死锁现象,最快速的解决方案把所有的互斥锁都改成一把递归锁,但是程序的效率会降低的
# 3:递归锁 效率低 但是解决死锁现象有奇效
# 4:互斥锁 效率高 但是多把锁容易出现死锁现象
# 5:一把互斥锁就够了