简体   繁体   English

为什么递归获取(可重入)锁?

[英]Why acquire a (reentrant) lock recursively?

ReentrantLock allows a thread to acquire the same lock recursively, so that a lock count is incremented and decremented on successive lock/unlock. ReentrantLock允许线程递归地获取相同的锁,以便锁定计数在连续锁定/解锁时递增和递减。 Whereas the lock count has to be decremented to zero before it is released to other threads. 锁定计数必须在释放到其他线程之前递减到零。

Why or under what circumstances would I write code to acquire a lock recursively? 为什么或在什么情况下我会编写代码以递归方式获取锁?

The only point I can see in having the feature is to make it convenient for us to write recursive code, where a method (which in the course of its execution acquires a lock) is called recursively. 我能看到的唯一一点就是使我们能够方便地编写递归代码,其中一个方法(在执行过程中获取一个锁)被递归调用。

Are there any other situations where recursive/repeated acquisition of a lock by a thread may be useful ? 是否有任何其他情况下线程的递归/重复获取锁可能有用?

Clarification of the question: 澄清问题:

  • Please ignore the lock being reentrant. 请忽略可重入的锁。 Just so happens that recursivity is provided by reentrant lock. 恰好相反,reentrant锁提供了递归性。
  • I am referring to the recursive feature of a lock 我指的是锁的递归功能
  • Please do not answer with why use reentrant lock. 请不要回答为什么使用折返锁。
  • Please do not answer with "recursivity is not the main feature of reentrant lock" 请不要回答“递归不是折返锁的主要特征”
  • I want to know what situations require the recursive acquisition of a lock, regardless if the lock is reentrant or not. 我想知道哪些情况需要递归获取锁,无论锁是否可重入。

Might as well search better: this should be helpful 不妨更好地搜索: 这应该是有帮助的

A use case for re-entrant locking: 可重入锁定的用例:

A (somewhat generic and contrived) example of an application for a re-entrant lock might be: 可重入锁定的应用程序的一个(有点通用和做作)示例可能是:

  1. You have some computation involving an algorithm that traverses a graph (perhaps with cycles in it). 你有一些涉及遍历图形的算法的计算(可能有周期)。 A traversal may visit the same node more than once due to the cycles or due to multiple paths to the same node. 由于循环或由于到同一节点的多个路径,遍历可以多次访问同一节点。

  2. The data structure is subject to concurrent access and could be updated for some reason, perhaps by another thread. 数据结构受并发访问的限制,可能由于某种原因(可能是另一个线程)进行更新。 You need to be able to lock individual nodes to deal with potential data corruption due to race conditions. 您需要能够锁定单个节点以处理由于竞争条件导致的潜在数据损坏。 For some reason (perhaps performance) you don't want to globally lock the whole data structure. 出于某种原因(可能是性能),您不希望全局锁定整个数据结构。

  3. You computation can't retain complete information on what nodes you've visited, or you're using a data structure that doesn't allow 'have I been here before' questions to be answered quickly. 您的计算无法保留您访问过的节点的完整信息,或者您使用的数据结构不允许“我以前在这里”问题可以快速回答。

  4. An example of this situation would be a simple implementation of Dijkstra's algorithm with a priority queue implemented as a binary heap or a breadth-first search using a simple linked list as a queue. 这种情况的一个例子是Dijkstra算法的简单实现,优先级队列实现为二进制堆,或者使用简单链接列表作为队列进行广度优先搜索。 In these cases, scanning the queue for existing insertions is O(N) and you may not want to do it on every iteration. 在这些情况下,扫描队列中的现有插入是O(N),您可能不希望在每次迭代时都这样做。

In this situation, keeping track of what locks you've already acquired is expensive. 在这种情况下,跟踪您已经获得的锁是昂贵的。 Assuming you want do the locking at the node level a re-entrant locking mechanism alleviates the need to tell whether you've visited a node before. 假设您希望在节点级别执行锁定,则可重入锁定机制可以减少判断您之前是否访问过节点的需要。 You can just blindly lock the node, perhaps unlocking it after you pop it off the queue. 您可以盲目地锁定节点,也许在将其从队列中弹出后将其解锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM