死锁实例

死锁实例

#coding=utf-8 
import time 
import threading 
class Account: 
  def __init__(self, _id, balance, lock): 
    self.id = _id 
    self.balance = balance 
    self.lock = lock 
  
  def withdraw(self, amount): 
    self.balance -= amount 
  
  def deposit(self, amount): 
    self.balance += amount 
  
  
def transfer(_from, to, amount): 
  if _from.lock.acquire():#锁住自己的账户 
    _from.withdraw(amount) 
    time.sleep(1)#让交易时间变长,2个交易线程时间上重叠,有足够时间来产生死锁 
    print 'wait for lock...'
    if to.lock.acquire():#锁住对方的账户 
      to.deposit(amount) 
      to.lock.release() 
    _from.lock.release() 
  print 'finish...'
  
a = Account('a',1000, threading.Lock()) 
b = Account('b',1000, threading.Lock()) 
threading.Thread(target = transfer, args = (a, b, 100)).start() 
threading.Thread(target = transfer, args = (b, a, 200)).start()

避免死锁,对锁进行排序

import threading
from contextlib import contextmanager
 
# Thread-local state to stored information on locks already acquired
_local = threading.local()
 
@contextmanager
def acquire(*locks):
  # Sort locks by object identifier
  locks = sorted(locks, key=lambda x: id(x))
 
  # Make sure lock order of previously acquired locks is not violated
  acquired = getattr(_local,'acquired',[])
  if acquired and max(id(lock) for lock in acquired) >= id(locks[0]):
    raise RuntimeError('Lock Order Violation')
 
  # Acquire all of the locks
  acquired.extend(locks)
  _local.acquired = acquired
 
  try:
    for lock in locks:
      lock.acquire()
    yield
  finally:
    # Release locks in reverse order of acquisition
    for lock in reversed(locks):
      lock.release()
    del acquired[-len(locks):]
import threading
x_lock = threading.Lock()
y_lock = threading.Lock()
 
def thread_1():
  while True:
    with acquire(x_lock, y_lock):
      print('Thread-1')
 
def thread_2():
  while True:
    with acquire(y_lock, x_lock):
      print('Thread-2')
 
t1 = threading.Thread(target=thread_1)
t1.daemon = True
t1.start()
 
t2 = threading.Thread(target=thread_2)
t2.daemon = True
t2.start()

哲学家吃饭问题

import threading
 
# The philosopher thread
def philosopher(left, right):
  while True:
    with acquire(left,right):
       print(threading.currentThread(), 'eating')
 
# The chopsticks (represented by locks)
NSTICKS = 5
chopsticks = [threading.Lock() for n in range(NSTICKS)]
 
# Create all of the philosophers
for n in range(NSTICKS):
  t = threading.Thread(target=philosopher,
             args=(chopsticks[n],chopsticks[(n+1) % NSTICKS]))
  t.start()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值