C++单例模式

在C++中,单例模式是一种确保一个类只有一个实例,并提供一个全局访问点来访问这个实例的设计模式。

单例模式中的饿汉式(Eager Initialization)和懒汉式(Lazy Initialization)是两种不同的实例化策略,它们在实例的创建时机、性能和线程安全性方面有所区别。

以下是一个单例模式的典型实现:

饿汉式(Eager Initialization)

  1. 实例创建时机

    • 在程序启动时或类加载时立即创建单例实例。
  2. 性能

    • 由于实例在程序开始时就已经创建,因此第一次调用 getInstance 方法时不需要进行任何同步操作,速度较快。
  3. 线程安全性

    • 饿汉式是线程安全的,因为实例在类加载时就已经创建,JVM(Java虚拟机)保证了类加载过程的线程安全性。
  4. 资源使用

    • 如果单例实例很大或者初始化过程很耗时,并且程序可能永远不使用这个实例,那么饿汉式可能会浪费资源。
  5. 实现

    • 通常通过静态常量实现。

在这种方法中,单例实例在程序开始时就被创建。

复制代码
class Singleton {
public:
    static Singleton& getInstance() {
        return instance;
    }

private:
    Singleton() {}
    static Singleton instance; // 静态常量
};

Singleton Singleton::instance; // 在程序启动时创建实例

懒汉式(Lazy Initialization)

  1. 实例创建时机

    • 在第一次调用 getInstance 方法时创建单例实例。
  2. 性能

    • 第一次调用 getInstance 时需要创建实例,可能会有一定的延迟,但如果实例很大或者初始化很耗时,那么这种方式可以延迟资源的使用,提高性能。
  3. 线程安全性

    • 基本的懒汉式实现不是线程安全的,因为在多线程环境中可能会有多个线程同时进入 if 判断并创建多个实例。
    • 为了保证线程安全,通常需要添加同步机制(如互斥锁),但这会降低性能。
  4. 资源使用

    • 懒汉式只在需要时创建实例,更加节省资源。
  5. 实现

    • 通常通过静态成员变量和同步代码块实现。
cpp 复制代码
class Singleton {
public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }

private:
    Singleton() {}
    static Singleton* instance; // 静态成员变量
};

Singleton* Singleton::instance = nullptr; // 初始时为空

为了使懒汉式线程安全,可以添加互斥锁:

cpp 复制代码
#include <mutex>

class Singleton {
public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            std::lock_guard<std::mutex> lock(mutex_);
            if (instance == nullptr) {
                instance = new Singleton();
            }
        }
        return instance;
    }

private:
    Singleton() {}
    static Singleton* instance; // 静态成员变量
    static std::mutex mutex_;  // 互斥锁
};

Singleton* Singleton::instance = nullptr;
std::mutex Singleton::mutex_;

懒汉式使用示例

cpp 复制代码
Student* Student::instance_ = NULL;
Student Student::staticInstance;

class Student: public people
{
public:
    static Student * instance(){return instance_;};

private:

    Student ();
    virtual ~Student ();

    static Student * instance_;
    static Student staticInstance;

    Student (const Student &);
    Student & operator = (const Student &);
};
Student ::Student ():
{
    instance_ = &staticInstance;
}
相关推荐
w2sfot19 小时前
Passing Arguments as an Object in JavaScript
开发语言·javascript·ecmascript
郝学胜-神的一滴19 小时前
避免使用非const全局变量:C++中的最佳实践 (C++ Core Guidelines)
开发语言·c++·程序人生
搞一搞汽车电子19 小时前
S32K3平台eMIOS 应用说明
开发语言·驱动开发·笔记·单片机·嵌入式硬件·汽车
总有刁民想爱朕ha20 小时前
车牌模拟生成器:Python3.8+Opencv代码实现与商业应用前景(C#、python 开发包SDK)
开发语言·python·数据挖掘
小菜全20 小时前
uniapp新增页面及跳转配置方法
开发语言·前端·javascript·vue.js·前端框架
人衣aoa20 小时前
Python编程基础(八) | 类
开发语言·python
晚云与城21 小时前
今日分享:C++ Stack和queue(栈与队列)
开发语言·c++
小莞尔21 小时前
【51单片机】【protues仿真】基于51单片机停车场的车位管理系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
张烫麻辣亮。21 小时前
golang-gin包
开发语言·golang·gin
yuluo_YX21 小时前
Go Style 代码风格规范
开发语言·后端·golang