【C++】C++ 单例模式总结(5种单例实现方法)

本文介绍了C++中的单例模式,包括其定义、应用场景及为何需要它。详细阐述了线程安全问题,并提供了多种实现方式,如懒汉式、饿汉式及使用std::call_once的方法。
该文章已生成可运行项目,

本文对 C++ 的单例模式进行简单介绍和实现。

参考:

  1. C++ 线程安全的单例模式总结(强烈建议阅读原文,本文相当于做了总结,留作学习,并添加了一种新的单例方法 std::call_once )

目录

1. 什么是单例模式

单例模式是指在整个系统生命周期内,保证一个类只能产生一个实例,确保该类的唯一性。

为什么需要单例模式

两个原因:

  1. 节省资源。一个类只有一个实例,不存在多份实例,节省资源。
  2. 方便控制。在一些操作公共资源的场景时,避免了多个对象引起的复杂操作。

但是在实现单例模式时,需要考虑到线程安全的问题。

线程安全

  • 什么是线程安全?

在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况。

  • 如何保证线程安全?
  1. 给共享的资源加把锁,保证每个资源变量每时每刻至多被一个线程占用。
  2. 让线程也拥有资源,不用去共享进程中的资源。如:使用threadlocal可以为每个线程维护一个私有的本地变量。

单例模式分类

单例模式可以分为 懒汉式饿汉式 ,两者之间的区别在于创建实例的时间不同。

  • 懒汉式

系统运行中,实例并不存在,只有当需要使用该实例时,才会去创建并使用实例。这种方式要考虑线程安全。

  • 饿汉式

系统一运行,就初始化创建实例,当需要时,直接调用即可。这种方式本身就线程安全,没有多线程的线程安全问题。

单例类的特点

  • 构造函数和析构函数为私有类型,目的是禁止外部构造和析构。
  • 拷贝构造函数和赋值构造函数是私有类型,目的是禁止外部拷贝和赋值,确保实例的唯一性。
  • 类中有一个获取实例的静态方法,可以全局访问。

2. 单例模式实现

单例的经典实现方式是「静态局部变量的懒汉单例」,推荐使用这种方式。

关于单例实现可以读下 stack overflow 上 这个回答

普通懒汉式单例(线程不安全)

这种情况是线程不安全的,不作详细介绍。

下面这几种实现都是线程安全的。

静态局部变量的懒汉单例(推荐)

这种方式在 C++11 下是线程安全的。这种单例实现方式称为 Meyer’s Singleton

头文件:

///////////////////  内部静态变量的懒汉实现  //////////////////

class Single
{

public:
    // 获取单实例对象
    static Single& GetInstance();
	
	// 打印实例地址
    void Print();

private:
    // 禁止外部构造
    Single();

    // 禁止外部析构
    ~Single();

    // 禁止外部拷贝构造
    Single(const Single &single) = delete;

    // 禁止外部赋值操作
    const Single &operator=(const Single &single) = delete;
};

源文件:

Single& Single::GetInstance()
{
    /**
     * 局部静态特性的方式实现单实例。
     * 静态局部变量只在当前函数内有效,其他函数无法访问。
     * 静态局部变量只在第一次被调用的时候初始化,也存储在静态存储区,生命周期从第一次被初始化起至程序结束止。
     */
    static Single single;
    return single;
}

void Single::Print()
{
    std::cout << "我的实例内存地址是:" << this << std::endl;
}

Single::Single()
{
    std::cout << "构造函数" << std::endl;
}

Single::~Single()
{
    std::cout << "析构函数" << std::endl;
}

但是,这种方法也有点问题:在多线程场景下还是有可能会存在线程安全的问题,因为多线程同时调用 GetInstance() 方法有可能还是会产生竞争。
解决这个问题的一种做法是:在程序的单线程启动阶段就调用 GetInstance() 方法。

经过大佬指正,上面这种说法是不正确的,在 C++11 中,静态局部变量这种方式天然是线程安全的,不存在线程不安全的问题。详见 stack overflow :Singleton instance declared as static variable of GetInstance method, is it thread-safe?

加锁的懒汉式单例

使用互斥锁保证线程安全。

方法1:返回普通指针

头文件:

///////////////////  加锁的懒汉式实现  //////////////////

class SingleInstance
{

public:
    // 获取单实例对象
    static SingleInstance *GetInstance();

    //释放单实例,进程退出时调用
    static void deleteInstance();
	
    // 打印实例地址
    void Print();

private:
    // 将其构造和析构成为私有的, 禁止外部构造和析构
    SingleInstance();
    ~SingleInstance();

    // 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值
    SingleInstance(const SingleInstance &signal);
    const SingleInstance &operator=(const SingleInstance &signal);

private:
    // 唯一单实例对象指针
    static SingleInstance *m_SingleInstance;
    static std::mutex m_Mutex;
};

源文件:

//初始化静态成员变量
SingleInstance *SingleInstance::m_SingleInstance = nullptr;
std::mutex SingleInstance::m_Mutex;

// 注意:不能返回指针的引用,否则存在外部被修改的风险!
SingleInstance * SingleInstance::GetInstance()
{

    //  这里使用了两个 if 判断语句的技术称为双检锁;好处是,只有判断指针为空的时候才加锁,
    //  避免每次调用 GetInstance的方法都加锁,锁的开销毕竟还是有点大的。
    if (m_SingleInstance == nullptr) 
    {
        std::unique_lock<std::mutex> lock(m_Mutex); // 加锁
        if (m_SingleInstance == nullptr)
        {
            volatile auto temp = new (std::nothrow) SingleInstance();
            m_SingleInstance = temp;
        }
    }

    return m_SingleInstance;
}

void SingleInstance::deleteInstance()
{
    std::unique_lock<std::mutex> lock(m_Mutex); // 加锁
    if (m_SingleInstance)
    {
        delete m_SingleInstance;
        m_SingleInstance = nullptr;
    }
}

void SingleInstance::Print()
{
	std::cout << "我的实例内存地址是:" << this << std::endl;
}

SingleInstance::SingleInstance()
{
    std::cout << "构造函数" << std::endl;
}

SingleInstance::~SingleInstance()
{
    std::cout << "析构函数" << std::endl;
}

方法2:返回智能指针

#include <iostream>
#include <memory>
#include <mutex>


class Singleton {

public:

    static std::shared_ptr<Singleton> getSingleton();

    void print() {
        std::cout << "Hello World." << std::endl;
    }

    ~Singleton() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }

private:

    Singleton() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

static std::shared_ptr<Singleton> singleton = nullptr;
static std::mutex singletonMutex;

std::shared_ptr<Singleton> Singleton::getSingleton() {
    if (singleton == nullptr) {
        std::unique_lock<std::mutex> lock(singletonMutex);
        if (singleton == nullptr) {
            volatile auto temp = std::shared_ptr<Singleton>(new Singleton());
            singleton = temp;
        }
    }
    return singleton;
}

使用 C++11 std::call_once 实现的懒汉单例

使用 C++11 std::call_once 实现的懒汉单例,C++11 线程安全。

#include <iostream>
#include <memory>
#include <mutex>

class Singleton {
public:
    static std::shared_ptr<Singleton> getSingleton();

    void print() {
        std::cout << "Hello World." << std::endl;
    }

    ~Singleton() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }

private:
    Singleton() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

static std::shared_ptr<Singleton> singleton = nullptr;
static std::once_flag singletonFlag;

std::shared_ptr<Singleton> Singleton::getSingleton() {
    std::call_once(singletonFlag, [&] {
        singleton = std::shared_ptr<Singleton>(new Singleton());
    });
    return singleton;
}

饿汉式单例

这种实现也是线程安全的。

头文件:

////////////////////////// 饿汉实现 /////////////////////

class Singleton
{
public:
    // 获取单实例
    static Singleton* GetInstance();

    // 释放单实例,进程退出时调用
    static void deleteInstance();
    
    // 打印实例地址
    void Print();

private:
    // 将其构造和析构成为私有的, 禁止外部构造和析构
    Singleton();
    ~Singleton();

    // 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值
    Singleton(const Singleton &signal);
    const Singleton &operator=(const Singleton &signal);

private:
    // 唯一单实例对象指针
    static Singleton *g_pSingleton;
};

源文件:

// 代码一运行就初始化创建实例 ,本身就线程安全
Singleton* Singleton::g_pSingleton = new (std::nothrow) Singleton();

Singleton* Singleton::GetInstance()
{
    return g_pSingleton;
}

void Singleton::deleteInstance()
{
    if (g_pSingleton)
    {
        delete g_pSingleton;
        g_pSingleton = nullptr;
    }
}

void Singleton::Print()
{
    std::cout << "我的实例内存地址是:" << this << std::endl;
}

Singleton::Singleton()
{
    std::cout << "构造函数" << std::endl;
}

Singleton::~Singleton()
{
    std::cout << "析构函数" << std::endl;
}

本文章已经生成可运行项目
### 单例模式的基本概念 单例模式是创建型设计模式的一种,其核心思想是确保一个类仅有一个实,并提供一个全局访问点来获取这个实。在程序运行期间,单例模式可以保证一个类只有一个实对象,并提供全局访问接口[^1][^2][^4]。 ### 实现方法 #### 饿汉式 饿汉式在程序开始时就创建实,线程安全,但可能会造成资源浪费。 ```cpp #include <iostream> class Singleton { public: static Singleton& getInstance() { return instance; } void showMessage() { std::cout << "Hello from Singleton!" << std::endl; } private: Singleton() { std::cout << "Singleton Constructor Called" << std::endl; } // 防止复制 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; static Singleton instance; }; Singleton Singleton::instance; int main() { Singleton& instance1 = Singleton::getInstance(); Singleton& instance2 = Singleton::getInstance(); instance1.showMessage(); if (&instance1 == &instance2) { std::cout << "Both instances are the same." << std::endl; } return 0; } ``` #### 懒汉式(非线程安全) 懒汉式在第一次使用时才创建实,但非线程安全。 ```cpp #include <iostream> class Singleton { public: static Singleton* getInstance() { if (instance == nullptr) { instance = new Singleton(); } return instance; } void showMessage() { std::cout << "Hello from Singleton!" << std::endl; } private: Singleton() { std::cout << "Singleton Constructor Called" << std::endl; } // 防止复制 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; static Singleton* instance; }; Singleton* Singleton::instance = nullptr; int main() { Singleton* instance1 = Singleton::getInstance(); Singleton* instance2 = Singleton::getInstance(); instance1->showMessage(); if (instance1 == instance2) { std::cout << "Both instances are the same." << std::endl; } return 0; } ``` #### 懒汉式(线程安全) 使用互斥锁保证线程安全,但会有一定的性能开销。 ```cpp #include <iostream> #include <mutex> class Singleton { public: static Singleton& getInstance() { std::lock_guard<std::mutex> lock(mutex); if (instance == nullptr) { instance = new Singleton(); } return *instance; } void showMessage() { std::cout << "Hello from Singleton!" << std::endl; } private: Singleton() { std::cout << "Singleton Constructor Called" << std::endl; } // 防止复制 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; static Singleton* instance; static std::mutex mutex; }; Singleton* Singleton::instance = nullptr; std::mutex Singleton::mutex; int main() { Singleton& instance1 = Singleton::getInstance(); Singleton& instance2 = Singleton::getInstance(); instance1.showMessage(); if (&instance1 == &instance2) { std::cout << "Both instances are the same." << std::endl; } return 0; } ``` #### 基于局部静态变量(C++11及以上) 简洁、安全且高效,推荐使用。 ```cpp #include <iostream> class Singleton { public: static Singleton& getInstance() { static Singleton instance; return instance; } void showMessage() { std::cout << "Hello from Singleton!" << std::endl; } // 防止复制 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; private: Singleton() { std::cout << "Singleton Constructor Called" << std::endl; } }; int main() { Singleton& instance1 = Singleton::getInstance(); Singleton& instance2 = Singleton::getInstance(); instance1.showMessage(); if (&instance1 == &instance2) { std::cout << "Both instances are the same." << std::endl; } return 0; } ``` ### 使用场景 - **资源管理**:如数据库连接池、文件系统操作等,避免多个实同时操作同一资源导致冲突。 - **配置信息**:如全局的配置文件管理,确保所有模块使用相同的配置信息。 - **日志记录**:保证所有日志信息都记录到同一个日志文件中。 ### 注意事项 - **线程安全**:在多线程环境下,需要确保的创建和访问是线程安全的,可采用互斥锁或局部静态变量的方式。 - **生命周期管理**:确保对象在整个程序生命周期内的正确性,避免内存泄漏。 - **可测试性**:单例模式可能会影响代码的可测试性,可考虑使用依赖注入等技术来提高可测试性。 - **避免滥用**:单例模式会引入全局状态,过度使用可能导致代码耦合度增加,难以维护和扩展。
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值