避免多线程进程中的死锁

发布于 2024-07-25 15:55:47 字数 32 浏览 6 评论 0原文

为了避免僵局,人们应该遵循哪些最佳实践/习惯用法?

What are the best practices/idioms should someone follow in order to avoid deadlocks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

半寸时光 2024-08-01 15:55:47

发生死锁必须满足四个条件

  1. 互斥条件:一种资源不能同时被多个进程使用

  2. 持有并等待条件:已经持有资源的进程可以请求新资源

  3. 无抢占条件:不能从持有该资源的进程中强行删除资源,只能通过进程的显式操作来释放资源

  4. 循环等待条件:两个或多个进程形成一个循环链,其中每个进程等待链中下一个进程持有的资源

至少避免其中一个,最好是更多,这样就不会有太多问题。

There are four conditions which must occur for deadlock to occur:

  1. Mutual exclusion condition: a resource that cannot be used by more than one process at a time

  2. Hold and wait condition: processes already holding resources may request new resources

  3. 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

  4. 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.

晨光如昨 2024-08-01 15:55:47

有一种所谓的银行家算法,用于避免死锁。 您还可以考虑使用 Watch Dog 来打破僵局。 这里还有一些有趣的点。

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.

忆沫 2024-08-01 15:55:47

避免死锁的规范技术是拥有锁层次结构。 确保所有线程以相同的顺序获取锁或其他资源。 这避免了线程 1 持有锁 A 并需要锁 B 而线程 2 持有锁 B 并需要锁 A 的死锁情况。使用锁层次结构,两个线程必须以相同的顺序获取锁(例如,A 在 B 之前) 。

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).

帅气尐潴 2024-08-01 15:55:47

最佳实践是为您的线程定义一个类,并在线程中仅使用此类中的非静态字段,这样您的线程就不会共享任何内存。
当然,为了避免死锁,您还可以避免使用信号量、临界区和互斥体。 如果你想避免死锁,越少越好。 不幸的是,如果某些内存或其他资源在两个线程之间共享,则需要这些,否则您将面临数据损坏的风险。

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.

不可一世的女人 2024-08-01 15:55:47

在进入临界区的各种方法中,信号量和互斥体是最流行的。

  • 信号量是一种等待机制,而互斥体是一种锁定机制,这个概念是最令人困惑的,但简而言之,激活互斥体的线程只能停用它。 考虑到这一点...

  • 不要允许任何进程锁定部分资源,如果一个进程需要 5 个资源,请等到所有资源都可用。

  • 如果你在这里使用信号量,你可以解锁/取消等待其他线程占用的资源。 我的意思是先发制人是另一个原因。

这2个据我来说是基本条件,常见的4个注意事项中剩下的2个可以与这些相关。

如果您不同意,请添加评论。 我已经迟到了,稍后我会添加更清晰的解释。

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.

These 2 according to me are the basic conditions, the remaining 2 of the common 4 precautions can be related to these.

If u dont agree ps add comments. I've gtg already late, I will later add a cleaner and clearer explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文