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

相关推荐
_不会dp不改名_4 小时前
python-opencv环境搭建
开发语言·python·opencv
HappyAcmen4 小时前
9.复盘API全套流程
开发语言·python
zh路西法4 小时前
【Linux 串口通信】基于 C++ 多线程的同步/异步串口实现
linux·运维·c++·python
charlie1145141914 小时前
通用GUI编程技术——图形渲染实战(四十五)——D3D12资源与堆管理:从上传到驻留
开发语言·3d·图形渲染·win32
不会C语言的男孩4 小时前
C++ Primer 第12章:动态内存
开发语言·c++
踏着七彩祥云的小丑4 小时前
Go学习第1天:入门
开发语言·学习·golang·go
thisiszdy5 小时前
<C++> 浅拷贝与深拷贝
c++
2023自学中5 小时前
Linux虚拟机 CMakeLists.txt:x86 与 ARM 双架构编译脚本
linux·c语言·c++·嵌入式
眠りたいです5 小时前
现代C++:C++17中的新库特性
开发语言·c++·c++20·c++17