什么是死锁? [英] What is a deadlock?

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

问题描述

编写多线程应用程序时,遇到的最常见问题之一是死锁。



我向社群提出的问题是:


  1. 僵局?


  2. 如何检测它们?


  3. 您处理吗?


  4. 最后,如何防止它们发生?



<当多个进程同时尝试访问同一资源时,会发生锁定



一个进程丢失,必须等待另一个进程完成。



当等待进程仍在进行时,会发生死锁



因此,一个例子:



资源A和资源B由进程X和进程Y使用




  • X开始使用A

  • X和Y尝试开始使用B

  • Y'wins'并获得B第一

  • 现在Y需要使用A

  • A被X锁定,等待Y



避免死锁的最好方法是避免进程以这种方式交叉。减少需要尽可能多地锁定任何内容。



在数据库中,避免在单个事务中对不同的表进行大量更改,避免触发器并切换到乐观/脏/ nolock尽可能多读。


When writing multi-threaded applications, one of the most common problems experienced are deadlocks.

My questions to the community are:

  1. What is a deadlock?

  2. How do you detect them?

  3. Do you handle them?

  4. And finally, how do you prevent them from occurring?

解决方案

A lock occurs when multiple processes try to access the same resource at the same time.

One process loses out and must wait for the other to finish.

A deadlock occurs when the waiting process is still holding on to another resource that the first needs before it can finish.

So, an example:

Resource A and resource B are used by process X and process Y

  • X starts to use A.
  • X and Y try to start using B
  • Y 'wins' and gets B first
  • now Y needs to use A
  • A is locked by X, which is waiting for Y

The best way to avoid deadlocks is to avoid having processes cross over in this way. Reduce the need to lock anything as much as you can.

In databases avoid making lots of changes to different tables in a single transaction, avoid triggers and switch to optimistic/dirty/nolock reads as much as possible.

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

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