• 单例模式(懒汉式,饿汉式,变体)


    单例模式,用于确保一个类只有一个实例,并提供一个全局访问点以访问该实例。

    饿汉式(Eager Initialization)

    程序启动时就创建实例

    #include 
    class SingletonEager 
    {
    private:
        static SingletonEager* instance;
        SingletonEager() {} // 私有构造函数
    
    public:
        static SingletonEager* getInstance() {
            return instance;
        }
    };
    
    SingletonEager* SingletonEager::instance = new SingletonEager; // 在程序启动时即创建实例
    
    int main() 
    {
        SingletonEager* instance1 = SingletonEager::getInstance();
        SingletonEager* instance2 = SingletonEager::getInstance();
        std::cout << (instance1 == instance2) << std::endl;  // 输出 1,两个指针变量的内容相同
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    懒汉式(Lazy Initialization)

    延迟初始化,即在第一次访问时才创建实例。

    缺点:不是线程安全的。因为它没有考虑多线程同时访问的情况。如果多个线程同时调用 getInstance() 方法,并且在 instance 还没有被初始化之前,它们可能会同时进入条件 if (!instance) 中,导致多次创建实例,这违反了单例模式的要求。

    #include 
    
    class SingletonLazy 
    {
    private:
        static SingletonLazy* instance;
        SingletonLazy() {} // 私有构造函数
    
    public:
        static SingletonLazy* getInstance() 
        {
            if (!instance) {
                instance = new SingletonLazy;
            }
            return instance;
        }
    };
    
    SingletonLazy* SingletonLazy::instance = nullptr;
    
    int main() {
        SingletonLazy* instance1 = SingletonLazy::getInstance();
        SingletonLazy* instance2 = SingletonLazy::getInstance();
        std::cout << (instance1 == instance2) << std::endl; // 输出 1,两个指针变量的内容相同
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    想要解决线程安全问题,需要做互斥操作,类似于下面这样,搞一个互斥锁

    class SingletonLazyThreadSafe {
    private:
        static SingletonLazyThreadSafe* instance;
        static std::mutex mutex;
        SingletonLazyThreadSafe() {} // 私有构造函数
    
    public:
        static SingletonLazyThreadSafe* getInstance() {
            std::lock_guard<std::mutex> lock(mutex);
            if (!instance) {
                instance = new SingletonLazyThreadSafe;
            }
            return instance;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    变体

    这种方式非常简洁,并且是线程安全的

    #include 
    
    class SingletonLazy 
    {
    private:
        SingletonLazy() {} // 私有构造函数
    
    public:
        static SingletonLazy* getInstance() 
        {
        	static SingletonLazy* instance;
    
            return instance;
        }
    };
    
    SingletonLazy* SingletonLazy::instance = nullptr;
    
    int main() 
    {
        SingletonLazy* instance1 = SingletonLazy::getInstance();
        SingletonLazy* instance2 = SingletonLazy::getInstance();
        std::cout << (instance1 == instance2) << std::endl; // 输出 1,两个指针变量的内容相同
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    APP 页面秒开优化方面总结~
    Leetcode刷题笔记5
    基于象鼻虫损害优化算法求解装箱问题问题(Matlab代码实现)
    Brachistochrone:使用变分法找到最快下降曲线
    两款开源的工作流引擎快速开发框架源码
    Linux——gdb调试时多进程切换方法(attach/follow-fork-mode)
    RHCE8 资料整理(七)
    蓝牙安全入门——两道CTF题目复现
    vue3+vite配置svg文件的全局使用(想怎么改颜色、宽高都可以)
    SSH远程登录:两台或多台服务器之间免密登录设置
  • 原文地址:https://blog.csdn.net/Motarookie/article/details/133109176