为什么 std::shared_ptr 使用原子 cpu 操作

发布于 2024-12-28 08:53:44 字数 153 浏览 4 评论 0原文

我无法理解为什么shared_ptr 使用原子CPU 指令...我无法弄清楚原因,因为它不是线程安全的。 有人可以解释一下吗?

如果你想知道我是如何知道它使用原子结构的:有一个来自 C++ 及其他版本的剪辑,Herb 和 Andrei 谈论了它,但他们从未提及为什么会这样。

I have a problem understanding why shared_ptr is using atomic cpu instructions... I cant figure out the reasons because it is NOT thread safe.
Can somebody please explain.

If you wonder know how I know that it uses atomic intstuructions: there was a clip from C++ and beyond where Herb and Andrei talk about it, but they never mention the reasons why is it like that.

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

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

发布评论

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

评论(2

小姐丶请自重 2025-01-04 08:53:44

shared_ptr 的任何实例都是多线程安全的。它指向的数据不是多线程安全的。请参阅

如果正确应用原子指令(通过竞争线程访问以相同顺序完成保护)是实现线程安全的一种方法。另一种方法是使用互斥体。

查看有关 BOOST 的类似问题:Is boost shared_ptr xxx 线程安全吗?

Any instance of shared_ptr is multi-thread safe. The data it points to is not multi-thread safe. See this.

The atomic instructions, if properly applied (protection done in the same order by competing thread access) is one way to implement thread safety. Another way is by use of mutexes.

See a similar question for BOOST: Is boost shared_ptr xxx thread safe?

没有你我更好 2025-01-04 08:53:44

Herb Sutter 刚刚在他的 gotw 95 中使用了 shared_ptr 作为一个很好的例子,他在那里详细阐述了设计决策:
https://herbsutter.com/2014 /01/13/gotw-95-solution-thread-safety-and-synchronization/

Herb Sutter just used shared_ptr as a nice example in his gotw 95, he goes there to elaborate on design decision:
https://herbsutter.com/2014/01/13/gotw-95-solution-thread-safety-and-synchronization/

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