手撕单例模式中的饿汉模式和懒汉模式,懒汉模式还要再多加一个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++ 单例写法。

相关推荐
闻道且行之1 小时前
TurboOCR:基于PP-OCRv6的极速Windows离线OCR工具,深度解析3.4GB依赖背后的技术架构
c++·人工智能·python·qt·机器学习·ocr
许彰午2 小时前
95_Python内存管理与垃圾回收
开发语言·python
多加点辣也没关系3 小时前
JavaScript|第13章:原始类型的方法
开发语言·javascript·ecmascript
এ慕ོ冬℘゜3 小时前
深入理解 JavaScript 事件体系:Window、鼠标与键盘事件详解
开发语言·javascript·okhttp
c238564 小时前
Bug 猎手入门指南
c++·算法·bug
chouchuang5 小时前
day-030-综合练习-笔记管理器
开发语言·笔记·python
云空5 小时前
《Three.js 3D实例大全》
开发语言·javascript·3d·three.js
techdashen6 小时前
Go 1.26 新增 `bytes.Buffer.Peek`:只看数据,不移动读取位置
开发语言·后端·golang
C137的本贾尼6 小时前
第七篇:消息队列(MQ)——就是个带存储的异步通信管道
java·开发语言·中间件
玖玥拾6 小时前
C++ 数据结构 八大基础排序算法专题
数据结构·c++·算法·排序算法