码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • c++——线程安全的单例


    方式1—静态局部变量实现单例

    线程安全,c++11之后支持。实现代码如下:

    class Singleton
    {
    public:
        static Singleton *getInstance()
        {
            static Singleton instance;
            return &instance;
        }
    private:
        Singleton() {}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    局部静态变量的初始化时机:

    • 发生在函数第一次被调用到局部静态变量定义语句时;

    静态局部变量的特点:

    • 在全局数据区分配内存;
    • 静态局部变量一般在声明时初始化,如果没有被显式初始化,会被程序自动初始化为0;

    在c++98/03标准中,不是线程安全的,实际实现代码如下:

    static Singleton& getInstance()
    {
        static bool initialized = false;
        static char buf[sizeof(Singleton)];
    
        if (!initialized) {
            initialized = true;
            new(&buf) Singleton();
        }
    
        return (*(reinterpret_cast( &buf)));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    c++11后做了改进,保证了线程安全,大概描述如下:

    such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.
    
    • 1

    主要实现方式:

    • 在当前线程执行到需要初始化变量的地方时,如果有其他线程正在初始化该变量,则阻塞当前线程,直到初始化完成为止。

    方式2—std::call_once

    线程安全,c++11之后支持。实现代码如下:

    class Singleton
    {
    public:
       static Singleton& getInstance()
       {
           static std::once_flag s_flag;
           std::call_once(s_flag,[&](){
            m_instance.reset(new Singleton());
           });
           return *m_instance;
       }
        
       ~ Singleton() = default;
        
    private:
       Singleton() = default;
       Singleton& operator=(const Singleton&) = delete;
       Singleton(const Singleton&) = delete;
        
    private:
       static std::unique_ptr m_instance;
    }
    
    std::unique_ptr Singleton::m_instance;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    猿创征文 第二季|业务总结 #「笔耕不辍」--生命不息,写作不止#
    用btcdeb工具学会Bitcoin Script中应当要学会的指令--中山大学软件工程学院专选课区块链原理与技术实验Lab7
    BoW - Bag of Words - 词袋模型
    canal1.1.7实战
    前端面试题:找出任意一个HTML中的所有不重复的标签
    13、【创业必备企业架构,可开发任意项目】SpringCloud大型企业分布式微服务云架构源码之MySQL 分组
    K8s中集成Heketi使用Glusterfs
    微信私域运营工具CRM
    【工具】无需下载和注册的免费内网穿透隧道工具:localhost.run
    java毕业设计琳琅天上超市管理系统mybatis+源码+调试部署+系统+数据库+lw
  • 原文地址:https://blog.csdn.net/www_dong/article/details/126294744
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号