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;
}
相关推荐
孞㐑¥4 小时前
Linux之Socket 编程 UDP
linux·服务器·c++·经验分享·笔记·网络协议·udp
黄雪超7 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice7 小时前
对象的finalization机制Test
java·开发语言·jvm
水木兰亭7 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
思则变7 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang8 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
¥-oriented8 小时前
【C#中路径相关的概念】
开发语言·c#
CoderCodingNo8 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法
恋猫de小郭9 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
JosieBook9 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea