手撕单例模式中的饿汉模式和懒汉模式,懒汉模式还要再多加一个C++11版本的

以下展示 C++ 中单例模式的三种常见实现:饿汉模式、传统线程安全的懒汉模式(双重检查锁定) 以及 C++11 版本的懒汉模式(使用 Magic Static)。

cpp 复制代码
#include <iostream>
#include <mutex>

// ----------------------------- 1. 饿汉模式 -----------------------------
// 在程序启动时即完成实例创建,天然线程安全。
// 缺点:无论是否使用都会占用资源。
class SingletonHungry {
public:
    static SingletonHungry* getInstance() {
        return &instance;
    }

    // 禁止拷贝和赋值
    SingletonHungry(const SingletonHungry&) = delete;
    SingletonHungry& operator=(const SingletonHungry&) = delete;

private:
    SingletonHungry() { std::cout << "Hungry Singleton created." << std::endl; }
    static SingletonHungry instance;   // 静态实例,全局初始化
};

// 静态成员变量定义(程序启动时初始化)
SingletonHungry SingletonHungry::instance;


// ----------------------------- 2. 传统懒汉模式(双重检查锁定) -----------------------------
// 第一次使用时才创建实例,通过互斥锁保证线程安全。
// 注意:由于内存可见性问题,双重检查锁在早期 C++ 中可能失效,需配合原子操作。
// 这里使用 std::mutex 和双重检查作为示例。
class SingletonLazy {
public:
    static SingletonLazy* getInstance() {
        if (instance == nullptr) {               // 第一次检查
            std::lock_guard<std::mutex> lock(mtx);
            if (instance == nullptr) {           // 第二次检查
                instance = new SingletonLazy();
            }
        }
        return instance;
    }

    SingletonLazy(const SingletonLazy&) = delete;
    SingletonLazy& operator=(const SingletonLazy&) = delete;

private:
    SingletonLazy() { std::cout << "Lazy Singleton (traditional) created." << std::endl; }
    static SingletonLazy* instance;
    static std::mutex mtx;
};

SingletonLazy* SingletonLazy::instance = nullptr;
std::mutex SingletonLazy::mtx;


// ----------------------------- 3. C++11 懒汉模式(Magic Static) -----------------------------
// 利用 C++11 标准保证:函数内静态局部变量的初始化是线程安全的。
// 代码最简洁,性能最优,推荐使用。
class SingletonCpp11 {
public:
    static SingletonCpp11& getInstance() {
        static SingletonCpp11 instance;   // 首次调用时初始化,C++11 保证线程安全
        return instance;
    }

    SingletonCpp11(const SingletonCpp11&) = delete;
    SingletonCpp11& operator=(const SingletonCpp11&) = delete;

private:
    SingletonCpp11() { std::cout << "Lazy Singleton (C++11) created." << std::endl; }
};


// ----------------------------- 测试代码 -----------------------------
int main() {
    std::cout << "=== 饿汉模式 ===" << std::endl;
    auto* hungry1 = SingletonHungry::getInstance();
    auto* hungry2 = SingletonHungry::getInstance();
    std::cout << "地址相同: " << (hungry1 == hungry2) << std::endl;

    std::cout << "\n=== 传统懒汉模式 ===" << std::endl;
    auto* lazy1 = SingletonLazy::getInstance();
    auto* lazy2 = SingletonLazy::getInstance();
    std::cout << "地址相同: " << (lazy1 == lazy2) << std::endl;

    std::cout << "\n=== C++11 懒汉模式 ===" << std::endl;
    auto& cpp111 = SingletonCpp11::getInstance();
    auto& cpp112 = SingletonCpp11::getInstance();
    std::cout << "地址相同: " << (&cpp111 == &cpp112) << std::endl;

    return 0;
}

关键说明

注意:C++11 标准规定:如果多个线程同时访问同一个函数内的静态局部变量,初始化过程是线程安全的,且只会执行一次。因此 static SingletonCpp11 instance; 是最推荐的现代 C++ 单例写法。

相关推荐
zh731437 分钟前
typephp的编译核心phpx原理解析
android·java·开发语言
warpdrivelabs3 小时前
Codex 开源 harness 全面了解
开发语言·人工智能
1000世界小札4 小时前
《大话数据结构》第9章精读:归并排序与快速排序完整 C++ 实现
数据结构·c++·算法
2601_956121977 小时前
背包基础篇(01、完全、分组、多重、混合)
c++·算法·动态规划
丰锋ff7 小时前
基于 Qt 的智能门禁系统(二)
开发语言·qt
fpcc7 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
SomeB1oody7 小时前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
mqiqe8 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim8 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情8 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股