Welcome

首页 / 软件开发 / .NET编程技术 / 多线程中如何避免死锁

多线程中如何避免死锁2013-08-12 cnblogs 一路转圈的雪人死锁是很讨厌的(虽然活锁更讨厌),如何避免死锁呢?

在两个线程间的循环等待是比较容易识别的,但是在死锁的形成中如果包含多个线程,那么就是难以发现的(现实中不少这种情况)。

首先来看看死锁形成的几个必要条件

1、互斥

2、等待

3、不可抢占

4、循环等待

除了完全避免多线程编程之外,如果要避免死锁,那么必须要使得上面这4个条件中有任意一个不满足。

1、互斥是大多数锁的一种固有性质,你没办法改变它。

2、如果程序持有的锁不会多于一个,那么就不会发生死锁问题。但是这通常也是不可能的。

3、不可抢占,线程中断和线程终止并非是实现这个功能的合适方式。

4、循环等待。通过在锁上施加一种顺序,并且要求线程按照这种顺序来获取锁。那么就不会发生循环的获取锁操作,也就不会发生死锁。这也是这四种条件中最容易消除的。

我们用一个简单的例子实现简单的锁分级。

第一种会有并发问题的写法,我们本意是通过形参fromAccount , toAccount 来表明锁的顺序,可这是做不到的。因此如果我们调换了一下传入的实参的顺序,就会产生死锁问题。

class BankAccount {public int id { get; set; }public decimal Balance { get; set; }public static void Transfer(BankAccount fromAccount, BankAccount toAccount, decimal amount){lock (fromAccount){if (fromAccount.Balance < amount){throw new Exception("Insufficient funds");}lock (toAccount){fromAccount.Balance -= amount;toAccount.Balance += amount;}}}}
我们可以通过比较id来保证顺序,同时这种写法不会引入新的开销。但是这种写法局限性比较大,不通用。

class BankAccount{public int id { get; set; }public decimal Balance { get; set; }public static void SafeTransfer(BankAccount fromAccount, BankAccount toAccount, decimal amount){if (fromAccount.id < toAccount.id){lock (fromAccount){lock (toAccount){transferMoney(fromAccount, toAccount, amount);}}}else if (fromAccount.id > toAccount.id){lock (toAccount){lock (fromAccount){TransferMoney(fromAccount, toAccount, amount);}}}}private static void TransferMoney(BankAccount fromAccount, BankAccount toAccount, decimal amount){if (fromAccount.Balance < amount){throw new Exception("Insufficient funds");}fromAccount.Balance -= amount;toAccount.Balance += amount;}}