多线程之旅:避免死锁——简单的锁分级(锁排序)

死锁是很讨厌的(虽然活锁更讨厌),如何避免死锁呢?

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

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

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;
        }

    }

 

 本质上是通过固定一种顺序,因此我们想到可以通过排序的方式使得可以接受多个锁,并且更通用。

    class MultiLocksHelper<T> where T :IComparable<T>
    {
        internal static void Enter(params T[] locks)
        {
            Array.Sort(locks);

            int i = 0;
            try
            {
                for (; i < locks.Length; i++)
                {
                    Monitor.Enter(locks[i]);
                }
            }
            catch
            {
                for(int j= i-1;j>0;j--)
                {
                    Monitor.Exit(locks[j]);
                }
                throw;
            }
        }

        internal static void Exit(params T[] locks)
        {
            Array.Sort(locks);

            for (int i = locks.Length - 1; i >= 0; i--)
            {
                Monitor.Exit(locks[i]);
            }
        }
    }

 

这时调用起来就很简单了,只有几行代码。

class BankAccount : IComparable<BankAccount>
    {
        public int id { get; set; }
        public decimal Balance { get; set; }

        public static void GenericSafeTransfer(BankAccount fromAccount, BankAccount toAccount, decimal amount)
        {
            MultiLocksHelper<BankAccount>.Enter(fromAccount, toAccount);

            try
            {
                TransferMoney(fromAccount, toAccount, amount);
            }
            finally
            {
                MultiLocksHelper<BankAccount>.Exit(fromAccount, toAccount);
            }

        }


        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;
        }


        public int CompareTo(BankAccount other)
        {
            if (this.id > other.id)
                return 1;
            else
                return -1;
        }
    }

 

 锁分级的复杂性:

如果要为锁指定级别,那么就需要规划并遵守一定的原则。我们很难从一开始就提出很完备的锁分级策略。也比较难估计哪些地方使用到锁。

 

转载于:https://www.cnblogs.com/lwzz/archive/2013/05/26/3100053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值