死锁的常见原因是什么?

编程入门 行业动态 更新时间:2024-10-26 22:23:20
本文介绍了死锁的常见原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

死锁的常见原因是什么?

本文发布于:2023-11-26 03:23:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:死锁   常见   原因

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!