为什么 std::shared_ptr 使用原子 cpu 操作
我无法理解为什么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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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?
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/