为什么std :: shared_ptr使用原子cpu操作 [英] why is std::shared_ptr using atomic cpu operations

查看:130
本文介绍了为什么std :: shared_ptr使用原子cpu操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解为什么shared_ptr使用原子cpu指令时遇到问题...我无法弄清原因,因为它不是线程安全的. 有人可以解释一下吗.

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.

如果您想知道我是怎么知道它使用原子构造的:有一个C ++剪辑,赫伯和安德烈(Jerb and Andrei)谈论它,但是他们从未提及它的原因.

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.

推荐答案

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

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.

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

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

这篇关于为什么std :: shared_ptr使用原子cpu操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆