目录
单例模式
保证类只有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享
单例模式的要点有三个:
- 单例类只能有一个实例
- 必须自行创建这个实例
- 必须自行向整个系统提供这个实例
注意点
- 实例控制: 单例模式会阻止其他对象实例化自己的单例对象的副本,从而确保所有对象都访问唯一实例
- 灵活性: 因为类控制实例化过程,所以类可以灵活更改实例化过程
- 开销: 虽然数量很少,但如果每次对象请求引用时都要检查是否存在类的实例,将仍然需要一些开销,这个问题可以通过静态初始化解决此问题。定义一个私有的静态指针instance,和一个公有的静态函数 GetInstance()。
优点:
- 在内存中只有一个对象,节省内存空间
- 避免频繁的创建销毁对象,可以提高性能
- 避免对共享资源的多重占用
- 可以全局访问
具体运用场景如:
- 设备管理器,系统中可能有多个设备,但是只有一个设备管理器,用于管理设备驱动;
- 数据池,用来缓存数据的数据结构,需要在一处写,多处读取或者多处写,多处读取;
基础要点
- 全局只有一个实例:static 特性,同时禁止用户自己声明并定义实例(把构造函数设为 private)
- 线程安全
- 禁止赋值和拷贝
- 用户通过接口获取实例:使用 static 类成员函数
懒汉
故名思义,不到万不得已就不会去实例化类,也就是说在第一次用到类实例的时候才会去实例化
在访问量较小时,采用懒汉实现。这是以时间换空间。
经典懒汉
#include <iostream> // version1: // with problems below: // 1. thread is not safe // 2. memory leak 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; int main(){ Singleton* instance = Singleton::get_instance(); Singleton* instance_2 = Singleton::get_instance(); return 0; }
运行的结果是
constructor called!
可以看到,获取了两次类的实例,却只有一次类的构造函数被调用,表明只生成了唯一实例,这是个最基础版本的单例实现,他有哪些问题呢?
- 线程安全的问题,当多线程获取单例时有可能引发竞态条件:第一个线程在if中判断
m_instance_ptr
是空的,于是开始实例化单例;同时第2个线程也尝试获取单例,这个时候判断m_instance_ptr
还是空的,于是也开始实例化单例;这样就会实例化出两个对象,这就是线程安全问题的由来; 解决办法:加锁- 内存泄漏. 注意到类中只负责new出对象,却没有负责delete对象,因此只有构造函数被调用,析构函数却没有被调用;因此会导致内存泄漏。解决办法: 使用共享指针;
加锁懒汉
#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的方法达到线程安全
饿汉
线程安全:最推荐的懒汉式单例(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&
才能获取对象。简单手写单例模式(面试用)
饿汉式 class Singleton { private static Singleton instance=new Singleton(); private Singleton(){} static Singleton getInstance() { return instance; } }
懒汉式 class Singleton { private static Singleton instance=null; private Singleton(){} static Singleton getInstance() { if(instance==null) instance=new Singleton(); return instance; } }