Then what's really happening is TWO different sections of memory are being allocated. It's done at one time,
 but it's two "logical" blocks. One is the int which stores the actual value, and the other is the control block,
 which stores all the shared_ptr "magic" that makes it work.
It is only the control block itself which is thread-safe.
shared_ptr<int> global_ptr = make_shared<int>(0);
void thread_fcn();
int main(int argc, char** argv)
{
    thread thread1(thread_fcn);
    thread thread2(thread_fcn);
    ...
    thread thread10(thread_fcn);
    chrono::milliseconds duration(10000);
    this_thread::sleep_for(duration);
    return;
}
void thread_fcn()
{
    // This is thread-safe and will work fine, though it's useless.  Many
    // short-lived pointers will be created and destroyed.
    for(int i = 0; i < 10000; i++)
    {
        shared_ptr<int> temp = global_ptr;
    }
    // This is not thread-safe.  While all the threads are the same, the
    // "final" value of this is almost certainly NOT going to be
    // number_of_threads*10000 = 100,000.  It'll be something else.
    for(int i = 0; i < 10000; i++)
    {
        *global = *global + 1;
    }
}
C++标准库为std::shared_ptr提供了一些重要的辅助函数,让这些智能指针可以以“原子操作”的方式获取值,设置值.
std::shared_ptr<my_data> p;
void process_global_data()
{
    std::shared_ptr<my_data> local=std::atomic_load(&p);
    process_data(local);
}
void update_global_data()
{
    std::shared_ptr<my_data> local(new my_data);
    std::atomic_store(&p,local);
}
So if you do: it will be thread safe.
//In thread 1
shared_ptr<myClass> private = atomic_load(&global);
...
//In thread 2
atomic_store(&global, make_shared<myClass>());...

shared_ptr 线程安全的更多相关文章

  1. shared_ptr的线程安全

    1.9 再论shared_ptr 的线程安全 虽然我们借shared_ptr 来实现线程安全的对象释放,但是shared_ptr 本身不是100% 线程安全的.它的引用计数本身是安全且无锁的,但对象的 ...

  2. 智能指针原理及实现(1)shared_ptr

    0.异常安全 C++没有内存回收机制,每次程序员new出来的对象需要手动delete,流程复杂时可能会漏掉delete,导致内存泄漏.于是C++引入智能指针,可用于动态资源管理,资源即对象的管理策略. ...

  3. shared_ptr的线程安全性

    一: All member functions (including copy constructor and copy assignment) can be called by multiple t ...

  4. C++11 shared_ptr 智能指针 的使用,避免内存泄露

    多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...

  5. 从零开始山寨Caffe·肆:线程系统

    不精通多线程优化的程序员,不是好程序员,连码农都不是. ——并行计算时代掌握多线程的重要性 线程与操作系统 用户线程与内核线程 广义上线程分为用户线程和内核线程. 前者已经绝迹,它一般只存在于早期不支 ...

  6. 从零开始山寨Caffe·叁:全局线程管理器

    你需要一个管家,随手召唤的那种,想吃啥就吃啥. ——设计一个全局线程管理器 一个机器学习系统,需要管理一些公共的配置信息,如何存储这些配置信息,是一个难题. 设计模式 MVC框架 在传统的MVC编程框 ...

  7. 如何用shared_ptr减少锁的争用

    在并发环境下锁的使用是家常便饭, 如何减少锁的使用是优化程序性能的一个方面. c++11里面新增了智能指针std::shared_ptr, 这个东西也许能给我们带来些启发. shared_ptr的一个 ...

  8. C++线程池

    之前一直在找一个开源的C++线程池库,找了很久也没有找到一个好用的,后来项目需要, 本想自己写一个,但是无意中在github上面找了一个采用boost库实现的threadpool,后来研究 了一下源码 ...

  9. shared_ptr和多线程

    前一篇文章写得实在太挫,重新来一篇. 多线程环境下生命周期的管理 多线程环境下,跨线程对象的生命周期管理会有什么挑战?我们拿生产者消费者模型来讨论这个问题. 实现一个简单的用于生产者消费者模型的队列 ...

随机推荐

  1. Guid与id区别

    一.产生Guid的方法 1.SqlServer中使用系统自带的NEWID函数: select NEWID() 2.C#中,使用Guid类型的NewGuid方法: Guid gid;           ...

  2. linux awk的使用

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...

  3. Linux下的五个查找命令:grep、find、locate、whereis、which

    原文转自 http://www.cnblogs.com/wanqieddy/archive/2011/07/15/2107071.html 1.grep grep(General Regular Ex ...

  4. C#中as用法

    在程序中,进行类型转换时常见的事,C#支持基本的强制类型转换方法,例如 Object obj1 = new NewType();NewType newValue = (NewType)obj1;这样强 ...

  5. 新冲刺Sprint3(第七天)

    一.Sprint介绍 商家功能模块持续更新着...... 二.Sprint周期 看板: 燃尽图:

  6. Java单例模式(转载)

    原文地址:1:http://www.cnblogs.com/hupp/p/4487521; 2:http://cantellow.javaeye.com/blog/838473 先罗列五种单例模式的实 ...

  7. unsigned char 类型

    在蓝牙4.0的开发中,很多数据类型都用到了 unsigned char ,我觉得用这个类型的一个原因是相比较于整型,它占的空间更少. 比如: unsigned char a = 1;  // 占1个字 ...

  8. 《HelloGitHub》之GitHub Bot

    起因 我在github上发起了一个开源项目:<HelloGitHub月刊>,内容是github上收集的好玩,容易上手的开源项目. 目的:因为兴趣是最好的老师,我希望月刊中的内容可以激发读者 ...

  9. SQL总结系列

    SQL总结系列 总结SQL基本知识.用法,并结合多年的应用对SQL有关的相关知识进行总结.希望这些分享能给大家带来一些帮助,如有不足或错误,请批评指正. 主要内容 1)编辑相关,包括:数据库的创建与删 ...

  10. 支持无限精度无限大数的类BigNumber实现

    介绍 本篇是MathAssist的第二篇,在前言中粗略地展示了MathAssist的“计算和证明”能力,本篇开始将详细介绍其实现原理. 从计算开始说起,要实现任意大数的计算器首先得有一个类支持大数运算 ...