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

相关推荐
卷无止境20 小时前
C++ 的Eigen 库全解析
c++
卷无止境20 小时前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴1 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18003 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴3 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨4 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint4568 天前
C++进阶(1)——前景提要
c++
夜悊8 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴8 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0018 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp