What are the best practices/idioms should someone follow in order to avoid deadlocks?
为了避免死锁,有人应该遵循哪些最佳做法/习惯用法?
6 个解决方案
#2
There are four conditions which must occur for deadlock to occur:
发生死锁必须发生四种情况:
-
Mutual exclusion condition: a resource that cannot be used by more than one process at a time
互斥条件:一次不能由多个进程使用的资源
-
Hold and wait condition: processes already holding resources may request new resources
保持和等待条件:已持有资源的进程可以请求新资源
-
No preemption condition: No resource can be forcibly removed from a process holding it, resources can be released only by the explicit action of the process
没有抢占条件:没有资源可以从持有它的进程中强制删除,资源只能通过进程的显式操作来释放
-
Circular wait condition: two or more processes form a circular chain where each process waits for a resource that the next process in the chain holds
循环等待条件:两个或多个进程形成一个循环链,其中每个进程等待链中下一个进程所持有的资源
Avoid at least one of these, and preferably more, and you shouldn't have too many problems.
至少避免其中一种,最好是更多,你不应该有太多问题。
#3
There is so called Banker's algorithm, for deadlock avoidance. Also you can consider the use of Watch Dog in order to break out form deadlock. Here also few interesting points.
有所谓的Banker算法,用于避免死锁。你也可以考虑使用Watch Dog来打破僵局。这里也有一些有趣的观点。
#4
The canonical technique for deadlock avoidance is to have a lock hierarchy. Make sure that all threads acquire locks or other resources in the same order. This avoids the deadlock scenario where thread 1 hold lock A and needs lock B while thread 2 holds lock B and needs lock A. With a lock hierarchy, both threads would have to acquire the locks in the same order (say, A before B).
避免死锁的规范技术是具有锁定层次结构。确保所有线程以相同的顺序获取锁或其他资源。这避免了死锁情况,其中线程1持有锁A并且需要锁B而线程2持有锁B并且需要锁A.使用锁层次结构,两个线程都必须以相同的顺序获取锁(例如,B之前的A) 。
#5
The best practice would be by defining a class for your thread and use only non-static fields from this class in your thread so your threads won't be sharing any memory.
Of course, to avoid deadlocks you could also avoid the use of semaphores, critical sections and mutexes. Less is better, if you want to avoid deadlocks. Unfortunately, these are required if some memory or other resource is shared between two threads or else you risk corruption of data.
最佳实践是为您的线程定义一个类,并在线程中仅使用此类中的非静态字段,这样您的线程就不会共享任何内存。当然,为避免死锁,您还可以避免使用信号量,关键部分和互斥锁。如果你想避免死锁,那就更好了。不幸的是,如果在两个线程之间共享某些内存或其他资源,则需要这些内容,否则您可能会损坏数据。
#6
Among the various methods to enter critical sections -- semaphores and mutexs are the most popular.
在进入关键部分的各种方法中 - 信号量和互斥量是最受欢迎的。
-
A semaphore is a waiting mechanism and mutex is a locking mechanism, well the concept is confusing to the most, but in short, a thread activating a mutex can only deactivate it. with this in mind...
信号量是一种等待机制,而互斥锁是一种锁定机制,这个概念最让人困惑,但简而言之,激活互斥锁的线程只能将其停用。考虑到这一点......
-
Dont allow any process to lock partial no of resources, if a process need 5 resources, wait until all the are available.
如果进程需要5个资源,请不要让任何进程锁定部分资源,等待所有资源都可用。
- if u use semaphore here, u can unblock/un-wait the resource occupied by other thread. by this i mean pre-emption is another reason.
如果你在这里使用信号量,你可以解锁/取消等待其他线程占用的资源。我的意思是先发制人是另一个原因。
These 2 according to me are the basic conditions, the remaining 2 of the common 4 precautions can be related to these.
根据我这2个基本条件,其余2个常见的4个注意事项可以与这些相关。
If u dont agree ps add comments. I've gtg already late, I will later add a cleaner and clearer explanation.
如果你不同意ps添加评论。我已经迟到了,我稍后会添加一个更清晰,更清晰的解释。
#1
Please see What are common reasons for deadlocks?
请查看死锁的常见原因是什么?
#2
There are four conditions which must occur for deadlock to occur:
发生死锁必须发生四种情况:
-
Mutual exclusion condition: a resource that cannot be used by more than one process at a time
互斥条件:一次不能由多个进程使用的资源
-
Hold and wait condition: processes already holding resources may request new resources
保持和等待条件:已持有资源的进程可以请求新资源
-
No preemption condition: No resource can be forcibly removed from a process holding it, resources can be released only by the explicit action of the process
没有抢占条件:没有资源可以从持有它的进程中强制删除,资源只能通过进程的显式操作来释放
-
Circular wait condition: two or more processes form a circular chain where each process waits for a resource that the next process in the chain holds
循环等待条件:两个或多个进程形成一个循环链,其中每个进程等待链中下一个进程所持有的资源
Avoid at least one of these, and preferably more, and you shouldn't have too many problems.
至少避免其中一种,最好是更多,你不应该有太多问题。
#3
There is so called Banker's algorithm, for deadlock avoidance. Also you can consider the use of Watch Dog in order to break out form deadlock. Here also few interesting points.
有所谓的Banker算法,用于避免死锁。你也可以考虑使用Watch Dog来打破僵局。这里也有一些有趣的观点。
#4
The canonical technique for deadlock avoidance is to have a lock hierarchy. Make sure that all threads acquire locks or other resources in the same order. This avoids the deadlock scenario where thread 1 hold lock A and needs lock B while thread 2 holds lock B and needs lock A. With a lock hierarchy, both threads would have to acquire the locks in the same order (say, A before B).
避免死锁的规范技术是具有锁定层次结构。确保所有线程以相同的顺序获取锁或其他资源。这避免了死锁情况,其中线程1持有锁A并且需要锁B而线程2持有锁B并且需要锁A.使用锁层次结构,两个线程都必须以相同的顺序获取锁(例如,B之前的A) 。
#5
The best practice would be by defining a class for your thread and use only non-static fields from this class in your thread so your threads won't be sharing any memory.
Of course, to avoid deadlocks you could also avoid the use of semaphores, critical sections and mutexes. Less is better, if you want to avoid deadlocks. Unfortunately, these are required if some memory or other resource is shared between two threads or else you risk corruption of data.
最佳实践是为您的线程定义一个类,并在线程中仅使用此类中的非静态字段,这样您的线程就不会共享任何内存。当然,为避免死锁,您还可以避免使用信号量,关键部分和互斥锁。如果你想避免死锁,那就更好了。不幸的是,如果在两个线程之间共享某些内存或其他资源,则需要这些内容,否则您可能会损坏数据。
#6
Among the various methods to enter critical sections -- semaphores and mutexs are the most popular.
在进入关键部分的各种方法中 - 信号量和互斥量是最受欢迎的。
-
A semaphore is a waiting mechanism and mutex is a locking mechanism, well the concept is confusing to the most, but in short, a thread activating a mutex can only deactivate it. with this in mind...
信号量是一种等待机制,而互斥锁是一种锁定机制,这个概念最让人困惑,但简而言之,激活互斥锁的线程只能将其停用。考虑到这一点......
-
Dont allow any process to lock partial no of resources, if a process need 5 resources, wait until all the are available.
如果进程需要5个资源,请不要让任何进程锁定部分资源,等待所有资源都可用。
- if u use semaphore here, u can unblock/un-wait the resource occupied by other thread. by this i mean pre-emption is another reason.
如果你在这里使用信号量,你可以解锁/取消等待其他线程占用的资源。我的意思是先发制人是另一个原因。
These 2 according to me are the basic conditions, the remaining 2 of the common 4 precautions can be related to these.
根据我这2个基本条件,其余2个常见的4个注意事项可以与这些相关。
If u dont agree ps add comments. I've gtg already late, I will later add a cleaner and clearer explanation.
如果你不同意ps添加评论。我已经迟到了,我稍后会添加一个更清晰,更清晰的解释。