死锁的常见原因是什么? [英] What are common reasons for deadlocks?

查看:104
本文介绍了死锁的常见原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

死锁很难找到而且很难移除。

Deadlocks are hard to find and very uncomfortable to remove.

如何在代码中找到死锁的错误源?有任何死锁模式吗?

How can I find error sources for deadlocks in my code? Are there any "deadlock patterns"?

在我的特殊情况下,它处理数据库,但是每个死锁都存在这个问题。

In my special case, it deals with databases, but this question is open for every deadlock.

推荐答案

更新:最近的MSDN文章用于识别并发问题的工具和技术,也许也很有趣

Update: This recent MSDN article, Tools And Techniques to Identify Concurrency Issues, might also be of interest

Stephen Toub在MSDN文章死锁监视器中,指出了以下四个死锁所必需的条件:发生:

Stephen Toub in the MSDN article Deadlock monitor states the following four conditions necessary for deadlocks to occur:


  • 有限数量的特定资源。对于C#监视器(使用lock关键字时使用的监视器)而言,此限制数是1,因为监视器是互斥锁(意味着一次只能有一个线程拥有一个监视器)。

  • A limited number of a particular resource. In the case of a monitor in C# (what you use when you employ the lock keyword), this limited number is one, since a monitor is a mutual-exclusion lock (meaning only one thread can own a monitor at a time).

拥有一种资源并请求另一种资源的能力。在C#中,这类似于在释放第一个锁之前先锁定一个对象,然后再锁定另一个对象,例如:

The ability to hold one resource and request another. In C#, this is akin to locking on one object and then locking on another before releasing the first lock, for example:


lock(a)
{
...
    lock(b)
    {
            ...
    }
}




  • 没有抢占能力。在C#中,这意味着一个线程无法强迫另一个线程释放锁。

    • No preemption capability. In C#, this means that one thread can't force another thread to release a lock.

      循环等待条件。这意味着有一个线程循环,每个线程都在等待下一个线程释放资源,然后才能继续。

      A circular wait condition. This means that there is a cycle of threads, each of which is waiting for the next to release a resource before it can continue.

      他继续解释说,避免死锁的方法是避免(或阻止)第四种情况。

      He goes on to explain that the way to avoid deadlocks is to avoid (or thwart) condition four.


      Joe Duffy讨论了几种避免和检测死锁的技术

      包括一种称为锁定调平的功能。
      在锁级别中,为锁分配了
      个数字值,并且线程必须
      只获取具有
      个数字大于已获取的
      个锁的锁。这样可以防止出现
      循环的可能性。在今天的
      典型软件应用程序中,
      经常很难做好,
      并且每次锁获取都未能遵循锁级别
      会导致
      死锁。

      这篇关于死锁的常见原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆