递归锁

发布于 2024-12-25 03:37:13 字数 138 浏览 0 评论 0原文

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

使用起来似乎很复杂且危险。我可以看到我们可以避免死锁(只要锁堆栈不溢出),但我们不想捕获这样的问题吗?

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

提前致谢。

Is there a use case for recursive locks? Is there a scenario that absolutely requires recursive locking.

Seems to be complicated and dangerous to use. I can see that we may avoid deadlocks (provided the lock stack doesn't overflow) but don't we want to catch such problems.

Maybe I'm missing something here. Any pointers are appreciated.

Thanks in advance.

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

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

发布评论

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

评论(1

荒芜了季节 2025-01-01 03:37:13

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

递归锁会杀了你

我也喜欢这句话:

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

不要使用递归互斥体。这类似于使用用过的避孕套进行性行为。

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

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

递归互斥体是一种黑客手段。使用它们并没有什么问题,
但它们是拐杖。腿断了还是图书馆断了?很好,使用
拐杖。但至少要知道你正在使用拐杖,以及为什么;和
偶尔检查一下腿(或图书馆)以确保您仍然
需要拐杖。如果没有痊愈,就去看医生,因为
那不行。当你别无选择时,使用并不可耻
拐杖...但是你不能靠拐杖跑得很好,而且你也会
放慢任何依赖你的人的速度。

递归互斥锁是构建线程支持原型的绝佳工具
在现有的库中,正是因为它可以让您推迟困难
部分:库的调用路径和数据依赖分析。但
出于同样的原因,永远记住,你还没有完成,直到
它们都消失了,所以你可以创建一个你引以为豪的库,
不会不必要地限制整个并发
应用程序。

I like the title of this Blog entry:

Recursive locks will kill you

I also like this quote:

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.

Finally, here's an extremely interesting article about how recursive locks got into Posix pthreads in the first place:

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.

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