gpt4 book ai didi

data-structures - 递归锁

转载 作者:行者123 更新时间:2023-12-04 07:13:52 25 4
gpt4 key购买 nike

有递归锁的用例吗?是否存在绝对需要递归锁定的场景。

使用起来似乎很复杂和危险。我可以看到我们可以避免死锁(前提是锁堆栈不会溢出),但我们不想捕捉这样的问题。

也许我在这里遗漏了一些东西。任何指针表示赞赏。

提前致谢。

最佳答案

我喜欢这个博客条目的标题:

Recursive locks will kill you

我也喜欢这句话:

http://www.thinkingparallel.com/2006/09/27/recursive-locks-a-blessing-or-a-curse/

Don’t use recursive mutexes. It’s akin to sex with used condoms.



最后,这里有一篇非常有趣的文章,介绍了递归锁最初是如何进入 Posix pthread 的:

http://groups.google.com/group/comp.programming.threads/msg/d835f2f6ef8aed99?hl=en&pli=1

Recursive mutexes are a hack. There's nothing wrong with using them, but they're a crutch. Got a broken leg or library? Fine, use the crutch. But at least be aware that you're using a crutch, and why; and once in a while check out the leg (or library) to be sure you still need the crutch. And if it's not healing up, go see a doctor, because that's just not OK. When you have no choice, there's no shame in using a crutch... but you can't run very well on a crutch, and you'll also be slowing down anyone who depends on you.

Recursive mutexes can be a great tool for prototyping thread support in an existing library, exactly because it lets you defer the hard part: the call path and data dependency analysis of the library. But for that same reason, always remember that you're not DONE until they're all gone, so you can produce a library you're proud of, that won't unnecessarily contrain the concurrency of the entire application.

关于data-structures - 递归锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8720783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com