设计模式之五:单例模式

有些对象只需要有一个,比如线程池、缓存和注册表等。

对比全局变量,其需要在程序开始就创建好对象,如果这个对象比较耗资源,而在后面的执行过程中又一直没有用到,就造成了浪费。

cpp 复制代码
class Singleton {
    private:
        static Singleton instance;
        Singleton(){}

    public
        static Singleton getInstance() {
        // 多线程环境下,可能先后判断为true
        if ( instance == null ) {
            instance = new Singleton();
        }
        return instance;
    }
}

双重检查锁

cpp 复制代码
// 解决指针没有析构和多线程问题
class Singleton{
    private:
        static std::shared_ptr<Singleton > instance;
    	static std::mutex m_mutex;
        Singleton(){}

    public
        static Singleton getInstance() {
        // 多线程环境下,可能先后判断为true
        if ( instance == null ) {
            std::lock_guard<std::mutex> lk(m_mutex);
            instance = std::shared_ptr<Singleton>(new             
                Singleton());
        }
        return instance;
    }
}

上述代码还存在问题是new Singleton()口语被抽象为下面的语句:

cpp 复制代码
memory = allocate();    //1:分配对象的内存空间
instance = memory;      //3:设置instance指向刚分配的内存地址(此时对象还未初始化)
new(instance);          //2:初始化对象

那么另一线程在判断是否为null的时候可能得到一个为完全初始化的instance。

cpp 复制代码
我们可以通过volatile对变量进行限制,防止指令重排
static std::shared_ptr<Singleton > volatile instance;

在Java中这样应该就可以了,c++好像还有点问题?具体的可以看下最后贴的参考文献。

最推荐的懒汉式单例模式(magic static)

cpp 复制代码
// 解决指针没有析构和多线程问题
class Singleton{
    private:
        Singleton(){}

    public
        static Singleton& getInstance() {
        static Singleton instrance;
        return instance;
    }
}

魔法静态变量是C++11的核心语言功能特性,提案:N2660 - Dynamic Initialization and Destruction with Concurrency, 最早在GCC2.3 / Clang2.9 / MSVC19.0等编译器得到支持。

这种方法又叫做 Meyers' SingletonMeyer's的单例, 是著名的写出《Effective C++》系列书籍的作者 Meyers 提出的。所用到的特性是在C++11标准中的Magic Static特性:

If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

如果当变量在初始化的时候,并发同时进入声明语句,并发线程将会阻塞等待初始化结束。

这样保证了并发线程在获取静态局部变量的时候一定是初始化过的,所以具有线程安全性。

单例模式volatile_单例模式 volatile_这瓜保熟么的博客-CSDN博客

C++ 单例模式_c++单例模式_WhiteTian的博客-CSDN博客

C++和双重检查锁定模式(DCLP)的风险_dclp认证_nodeathphoenix的博客-CSDN博客

相关推荐
渔舟小调1 小时前
P11 | 收藏与行程:用户行为类接口的设计模式
数据库·设计模式·oracle
小程故事多_803 小时前
从基础Agent到复杂工作流,LangGraph如何用状态机重构智能体开发
人工智能·设计模式·重构·aigc·ai编程
hypoy3 小时前
Claude Code 的 1M Context 怎么用:一篇官方文章的读后整理
设计模式·claude
IT 行者6 小时前
软件设计模式会不会是制约大模型编程的障碍?
设计模式·ai编程
t***5446 小时前
还有哪些设计模式适合现代C++
开发语言·c++·设计模式
t***5446 小时前
如何在现代C++项目中有效应用这些设计模式
开发语言·c++·设计模式
贵慜_Derek7 小时前
我们能从 DeerFlow 学到哪些优秀的技术架构设计
人工智能·设计模式·架构
Q741_1477 小时前
设计模式之装饰器模式 理论总结 C++代码实战
c++·设计模式·装饰器模式
无籽西瓜a7 小时前
【西瓜带你学设计模式 | 第十八期 - 命令模式】命令模式 —— 请求封装与撤销实现、优缺点与适用场景
java·后端·设计模式·软件工程·命令模式
studyForMokey8 小时前
【Android面试】设计模式专题
android·设计模式·面试