单例模式(Singleton Pattern)是设计模式中最简单的形式之一,其目的是使得类的一个对象成为系统中的唯一实例。
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一对象的方式,可以直接访问,不需要实例化该类的对象。
文章开头先给介绍单例模式的流程图和摘录的几篇单例模式文章的解释:
单例模式分为懒汉模式、饿汉模式、单例模板三种模式。
懒汉模式:懒汉模式不到万不得已就不会去实例化类,也就是说在第一次用到类实例的时候才会去实例化(懒汉本身是线程不安全的)。
饿汉模式:饿汉模式在单例类定义的时候就进行实例化(本身就是线程安全的)。
单例模板:在某些情况下有多个单例,如果都按照这种方式的话,实际上是一种重复。如果单例复用其代码实现多个单例, 可以考虑使用模板技术或者继承的方法。
特点与选择:
懒汉模式:在访问量较小时,采用懒汉实现。这是以时间换空间。
饿汉模式:由于要进行线程同步,所以在访问量比较大,或者可能访问的线程比较多时,采用饿汉实现,可以实现更好的性能。这是以空间换时间。

单例模板:多个单例模式实现,使用模板或者继承解决多个同样代码重复问题。
本文作者原创,未经允许请勿转载,本帖原创请勿照抄。
C++ 深入理解单例模式目录
3. 最推荐的懒汉式单例(magic static )——局部静态变量
一、饿汉单例模式
饿汉模式内容比较少,所以先写出来。主要作用:全局只能初始化一次,在项目初始化过程中就实现实例化单例,所以多线程等操作不需要涉及单例的初始化,所以饿汉模式是线程安全。
//饿汉单例模式
class Singelton {
private:
Singelton() {
m_count++;
printf("Singelton begin\n");
Sleep(5000); // 加sleep为了放大效果
printf("Singelton end\n");
}
static Singelton *single;
public:
static Singelton *GetSingelton();
//static void print();
static int m_count;
};
// 饿汉模式的关键:初始化即实例化
Singelton *Singelton::single = new Singelton;
int Singelton::m_count = 0;
Singelton *Singelton::GetSingelton() {
// 不再需要进行实例化
//if(single == nullptr){
// single = new Singelton;
//}
return single;
}
/*
void Singelton::print() {
cout << m_count << endl;
}
// 回调函数
void threadFunc(void *p) {
DWORD id = GetCurrentThreadId(); // 获得线程id
cout << id << endl;
Singelton::GetSingelton()->print(); // 构造函数并获得实例,调用静态成员函数
}
*/
int main() {
system("pause");
return 0;
}
可以看到
二、懒汉单例模式
这里1和2提供参考不建议使用,建议使用第3种方法的单例模式。优势:static 特性,全局唯一;线程安全,并发不会造成多个初始化;private私有,禁止赋值和拷贝;接口获取实例,使用 static 类成员函数。前两种方法只提供参考。
- 全局只有一个实例:static 特性,同时禁止用户自己声明并定义实例(把构造函数设为 private)
- 线程安全
- 禁止赋值和拷贝
- 用户通过接口获取实例:使用 static 类成员函数
1. 有缺陷的懒汉式
懒汉式(Lazy-Initialization)的方法是直到使用时才实例化对象,也就说直到调用get_instance() 方法的时候才 new 一个单例的对象。好处是如果被调用就不会占用内存。
#include "pch.h"
#include <iostream>
#include <string>
#include <memory> // shared_ptr
#include <mutex> // mutex
#include <process.h>
#include <windows.h>
using namespace std;
//有缺陷的懒汉模式
class Singleton {
private:
Singleton() {
std::cout << "constructor called!" << std::endl;
}
Singleton(Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
static Singleton* m_instance_ptr;
public:
~Singleton() {
std::cout << "destructor called!" << std::endl;
}
static Singleton* get_instance() {
if (m_instance_ptr == nullptr) {
m_instance_ptr = new Singleton;
}
return m_instance_ptr;
}
void use() const { std::cout << "in use" << std::endl; }
};
Singleton* Singleton::m_instance_ptr = nullptr;
运行的结果是
constructor called!
可以看到,获取了两次类的实例,却只有一次类的构造函数被调用,表明只生成了唯一实例,这是个最基础版本的单例实现,他有哪些问题呢?
线程安全的问题,当多线程获取单例时有可能引发竞态条件:第一个线程在if中判断 m_instance_ptr
是空的,于是开始实例化单例;同时第2个线程也尝试获取单例,这个时候判断m_instance_ptr
还是空的,于是也开始实例化单例;这样就会实例化出两个对象,这就是线程安全问题的由来; 解决办法:加锁
内存泄漏. 注意到类中只负责new出对象,却没有负责delete对象,因此只有构造函数被调用,析构函数却没有被调用;因此会导致内存泄漏。解决办法: 使用共享指针;
因此,这里提供一个改进的,线程安全的、使用智能指针的实现;
2. 线程安全、内存安全的懒汉式单例 (智能指针,锁)
#include <iostream>
#include <memory> // shared_ptr
#include <mutex> // mutex
// version 2:
// with problems below fixed:
// 1. thread is safe now
// 2. memory doesn't leak
class Singleton{
public:
typedef std::shared_ptr<Singleton> Ptr;
~Singleton(){
std::cout<<"destructor called!"<<std::endl;
}
Singleton(Singleton&)=delete;
Singleton& operator=(const Singleton&)=delete;
static Ptr get_instance(){
// "double checked lock"
if(m_instance_ptr==nullptr){
std::lock_guard<std::mutex> lk(m_mutex);
if(m_instance_ptr == nullptr){
m_instance_ptr = std::shared_ptr<Singleton>(new Singleton);
}
}
return m_instance_ptr;
}
private:
Singleton(){
std::cout<<"constructor called!"<<std::endl;
}
static Ptr m_instance_ptr;
static std::mutex m_mutex;
};
// initialization static variables out of class
Singleton::Ptr Singleton::m_instance_ptr = nullptr;
std::mutex Singleton::m_mutex;
int main(){
Singleton::Ptr instance = Singleton::get_instance();
Singleton::Ptr instance2 = Singleton::get_instance();
return 0;
}
运行结果如下,发现确实只构造了一次实例,并且发生了析构。
constructor called!
destructor called!
shared_ptr和mutex都是C++11的标准,以上这种方法的优点是
基于 shared_ptr, 用了C++比较倡导的 RAII思想,用对象管理资源,当 shared_ptr 析构的时候,new 出来的对象也会被 delete掉。以此避免内存泄漏。
加了锁,使用互斥量来达到线程安全。这里使用了两个 if判断语句的技术称为双检锁;好处是,只有判断指针为空的时候才加锁,避免每次调用 get_instance的方法都加锁,锁的开销毕竟还是有点大的。
不足之处在于: 使用智能指针会要求用户也得使用智能指针,非必要不应该提出这种约束; 使用锁也有开销; 同时代码量也增多了,实现上我们希望越简单越好。
还有更加严重的问题,在某些平台(与编译器和指令集架构有关),双检锁会失效!具体可以看这篇文章,解释了为什么会发生这样的事情。
因此这里还有第三种的基于 Magic Staic的方法达到线程安全
3. 最推荐的懒汉式单例(magic static )——局部静态变量
#include <iostream>
class Singleton
{
public:
~Singleton(){
std::cout<<"destructor called!"<<std::endl;
}
Singleton(const Singleton&)=delete;
Singleton& operator=(const Singleton&)=delete;
static Singleton& get_instance(){
static Singleton instance;
return instance;
}
private:
Singleton(){
std::cout<<"constructor called!"<<std::endl;
}
};
int main(int argc, char *argv[])
{
Singleton& instance_1 = Singleton::get_instance();
Singleton& instance_2 = Singleton::get_instance();
return 0;
}
运行结果:
constructor called!
destructor called!
这种方法又叫做 Meyers' SingletonMeyer's的单例, 是著名的写出《Effective C++》系列书籍的作者 Meyers 提出的。所用到的特性是在C++11标准中的Magic Static特性:
If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.
如果当变量在初始化的时候,并发同时进入声明语句,并发线程将会阻塞等待初始化结束。
这样保证了并发线程在获取静态局部变量的时候一定是初始化过的,所以具有线程安全性。
C++静态变量的生存期 是从声明到程序结束,这也是一种懒汉式。
这是最推荐的一种单例实现方式:
- 通过局部静态变量的特性保证了线程安全 (C++11, GCC > 4.3, VS2015支持该特性);
- 不需要使用共享指针,代码简洁;
- 注意在使用的时候需要声明单例的引用
Single&
才能获取对象。
3. 单例的模板
在
3.1 CRTP 奇异递归模板模式实现
代码示例如下:
/* 代码参考改编自https://www.cnblogs.com/sunchaothu/p/10389842.html */
//CRTP 奇异递归模板模式实现
template<typename T>
class Singleton {
public:
static T& get_instance() {
static T instance;
return instance;
}
virtual ~Singleton() {
std::cout << "destructor called!" << std::endl;
}
Singleton(const Singleton&) = delete;
Singleton& operator =(const Singleton&) = delete;
protected:
Singleton() {
std::cout << "constructor called!" << std::endl;
}
};
class DerivedSingle :public Singleton<DerivedSingle> {
// !!!! attention!!!
// needs to be friend in order to
// access the private constructor/destructor
friend class Singleton<DerivedSingle>;
public:
DerivedSingle(const DerivedSingle&) = delete;
DerivedSingle& operator =(const DerivedSingle&) = delete;
private:
DerivedSingle() = default;
};
class DerivedAfter :public Singleton<DerivedAfter> {
// !!!! attention!!!
// needs to be friend in order to
// access the private constructor/destructor
friend class Singleton<DerivedAfter>;
public:
DerivedAfter(const DerivedAfter&) = delete;
DerivedAfter& operator =(const DerivedAfter&) = delete;
private:
DerivedAfter() = default;
};
int main() {
//CRTP 奇异递归模板模式实现
DerivedSingle& instance1 = DerivedSingle::get_instance();
DerivedSingle& instance2 = DerivedSingle::get_instance();
DerivedAfter& instances1 = DerivedAfter::get_instance();
DerivedAfter& instances2 = DerivedAfter::get_instance();
system("pause");
return 0;
}
运行结果:
以上实现一个单例的模板基类,使用方法如例子所示意,子类需要将自己作为模板参数T 传递给 Singleton<T>
模板; 同时需要将基类声明为友元,这样才能调用子类的私有构造函数。
基类模板的实现要点是:
- 构造函数需要是 protected,这样子类才能继承;
- 使用了奇异递归模板模式CRTP(Curiously recurring template pattern)
- get instance 方法和 static local方法一个原理。
- 在这里基类的析构函数可以不需要 virtual ,因为子类在应用中只会用 Derived 类型,保证了析构时和构造时的类型一致
3.2 函数模板返回引用
这种方式确实非常简洁,同时类仍然具有一般类的特点而不受限制,当然也因此失去了单例那么强的约束(禁止赋值、构造和拷贝构造)。这里把函数命名为 get_global()
是为了强调,这里可以通过这种方式获取得到单例最重要的全局变量特性;但是并不是单例的模式。
/* 代码参考改编自https://www.cnblogs.com/sunchaothu/p/10389842.html */
//函数模板返回引用
class A
{
public:
A() {
std::cout << "constructor" << std::endl;
}
~A() {
std::cout << "destructor" << std::endl;
}
};
template<typename T>
T& get_global() {
static T instance;
return instance;
}
template<typename S>
S& get_globals() {
static S instances;
return instances;
}
int main() {
A& instance_1 = get_global<A>();
A& instance_2 = get_global<A>();
A& instances_1 = get_globals<A>();
A& instances_2 = get_globals<A>();
system("pause");
return 0;
}
运行结果:
4. 单例模式应用场景
应用场景:你需要系统中只有唯一 一个实例存在的类的全局变量的时候才使用单例
应用需求:越小越好,越简单越好,线程安全,内存不泄露