multithreading 避免多线程进程中的死锁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1043928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid deadlocks in a multithreaded process
提问by Nizar Grira
What are the best practices/idioms should someone follow in order to avoid deadlocks?
为了避免僵局,人们应该遵循哪些最佳实践/习惯用法?
回答by Mitch Wheat
Please see What are common reasons for deadlocks?
请参阅死锁的常见原因是什么?
回答by akatkinson
There are four conditionswhich 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.
至少避免其中一种,最好避免更多,这样您就不应该有太多问题。
回答by Artem Barger
回答by Keith Smith
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 的死锁场景。 使用锁层次结构,两个线程必须以相同的顺序获取锁(例如,A 在 B 之前) .
回答by Wim ten Brink
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.
最佳实践是为您的线程定义一个类,并在您的线程中仅使用此类中的非静态字段,这样您的线程就不会共享任何内存。
当然,为了避免死锁,您也可以避免使用信号量、临界区和互斥锁。如果您想避免死锁,则越少越好。不幸的是,如果在两个线程之间共享某些内存或其他资源,则需要这些,否则您可能会面临数据损坏的风险。
回答by vks
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.
- 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.
信号量是一种等待机制,互斥量是一种锁定机制,这个概念最容易混淆,但简而言之,激活互斥量的线程只能停用它。考虑到这一点......
不允许任何进程锁定部分资源,如果一个进程需要 5 个资源,请等待所有资源都可用。
- 如果你在这里使用信号量,你可以解除阻塞/解除等待其他线程占用的资源。我的意思是先发制人是另一个原因。
These 2 according to me are the basic conditions, the remaining 2 of the common 4 precautions can be related to these.
这2个按我说是基本情况,常见的4个注意事项中剩下的2个可以和这些有关。
If u dont agree ps add comments. I've gtg already late, I will later add a cleaner and clearer explanation.
如果你不同意 ps 添加评论。我已经迟到了 gtg,我稍后会添加一个更清晰、更清晰的解释。