在C++中,单例模式是一种确保一个类只有一个实例,并提供一个全局访问点来访问这个实例的设计模式。
单例模式中的饿汉式(Eager Initialization)和懒汉式(Lazy Initialization)是两种不同的实例化策略,它们在实例的创建时机、性能和线程安全性方面有所区别。
以下是一个单例模式的典型实现:
饿汉式(Eager Initialization)
-  
实例创建时机:
- 在程序启动时或类加载时立即创建单例实例。
 
 -  
性能:
- 由于实例在程序开始时就已经创建,因此第一次调用 
getInstance方法时不需要进行任何同步操作,速度较快。 
 - 由于实例在程序开始时就已经创建,因此第一次调用 
 -  
线程安全性:
- 饿汉式是线程安全的,因为实例在类加载时就已经创建,JVM(Java虚拟机)保证了类加载过程的线程安全性。
 
 -  
资源使用:
- 如果单例实例很大或者初始化过程很耗时,并且程序可能永远不使用这个实例,那么饿汉式可能会浪费资源。
 
 -  
实现:
- 通常通过静态常量实现。
 
 
在这种方法中,单例实例在程序开始时就被创建。
class Singleton {
public:
    static Singleton& getInstance() {
        return instance;
    }
private:
    Singleton() {}
    static Singleton instance; // 静态常量
};
Singleton Singleton::instance; // 在程序启动时创建实例
 
懒汉式(Lazy Initialization)
-  
实例创建时机:
- 在第一次调用 
getInstance方法时创建单例实例。 
 - 在第一次调用 
 -  
性能:
- 第一次调用 
getInstance时需要创建实例,可能会有一定的延迟,但如果实例很大或者初始化很耗时,那么这种方式可以延迟资源的使用,提高性能。 
 - 第一次调用 
 -  
线程安全性:
- 基本的懒汉式实现不是线程安全的,因为在多线程环境中可能会有多个线程同时进入 
if判断并创建多个实例。 - 为了保证线程安全,通常需要添加同步机制(如互斥锁),但这会降低性能。
 
 - 基本的懒汉式实现不是线程安全的,因为在多线程环境中可能会有多个线程同时进入 
 -  
资源使用:
- 懒汉式只在需要时创建实例,更加节省资源。
 
 -  
实现:
- 通常通过静态成员变量和同步代码块实现。
 
 
class Singleton {
public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }
private:
    Singleton() {}
    static Singleton* instance; // 静态成员变量
};
Singleton* Singleton::instance = nullptr; // 初始时为空
 
为了使懒汉式线程安全,可以添加互斥锁:
#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_;
 
懒汉式使用示例
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;
} 
 
 
 
 










