本文介绍了在实时应用程序中检测死锁的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 在实时多线程应用程序中检测死锁的方法有哪些?

  1. What are the ways to detect deadlocks in a live multi-threaded application?

如果发现死锁,是否有任何方法可以解决死锁,而无需关闭/重新启动应用程序?

If we found there is a deadlock, are there any ways to resolve it, without taking down/restarting the application?

推荐答案

有两种流行的检测死锁的方法.

There are two popular ways to detect deadlocks.

一种方法是让线程设置检查点.例如,如果您有一个带有工作循环的线程,则可以在工作开始时设置一个计时器,该计时器的设置时间比您认为工作可能需要的时间长.如果计时器触发,则假定线程已死锁.工作完成后,您取消计时器.

One is to have threads set checkpoints. For example, if you have a thread that has a work loop, you set a timer at the beginning of doing work that's set for longer than you think the work could possibly take. If the timer fires, you assume the thread is deadlocked. When the work is done, you cancel the timer.

另一种(有时结合使用)是使线程可能阻塞的事情跟踪线程可能拥有的其他资源.当其他线程以相反的顺序获取那些锁时,这可以直接检测到尝试获取一个锁而同时持有另一把锁.

Another (sometimes used in combination) is to have things that a thread might block on track what other resources a thread might hold. This can directly detect an attempt to acquire one lock while holding another one when other threads have acquired those locks in the opposite order.

这甚至可以检测到没有实际发生死锁的死锁风险.如果一个线程先获得锁A,然后获得B,而另一个线程先获得锁B,然后获得A,则除非死锁重叠,否则没有死锁.但是这种方法可以检测到它.

This can even detect deadlock risk without the deadlock actually occurring. If one thread acquires lock A then B and another acquires lock B then A, there is no deadlock unless they overlap. But this method can detect it.

高级死锁检测通常仅在调试期间使用.除了对应用程序进行编码以检查每个阻塞锁是否存在可能的死锁并知道如果发生死锁该怎么办之外,死锁后唯一可以做的就是将应用程序拆除.您不能盲目地释放锁,因为它们保护的资源可能处于不一致状态.

Advanced deadlock detection is typically only used during debugging. Other than coding the application to check each blocking lock for a possible deadlock and knowing what to do if it happens, the only thing you can do after a deadlock is tear the application down. You can't release locks blindly because the resources they protect may be in an inconsistent state.

有时,您故意编写知道会死锁的代码,并专门对其进行编码,以避免出现问题.例如,如果您知道许多线程都使用了锁A,然后尝试获取锁B,而其他一些线程需要这样做,那么您可以对其进行编码,以进行非阻塞尝试来锁定B,并在失败时释放锁A.

Sometimes you deliberately write code that you know can deadlock and specifically code it to avoid the problem. For example, if you know lots of threads take lock A and then try to acquire lock B, and some other thread needs to do the reverse, you can code it do a non-blocking attempt to lock B and release lock A if it fails.

通常,花很多精力使不可能的死锁比使代码检测并解决死锁更为有用.

Typically, it's more useful to spend your effort making deadlocks impossible rather than making the code detect and work around deadlocks.

这篇关于在实时应用程序中检测死锁的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 17:26