单例设计模式(懒汉模式、饿汉模式)C++
2016-09-29 23:57
423 查看
单例模式:全局唯一实例,提供一个很容易获取这个实例的接口
线程安全的单例:
懒汉模式(Lazy Loading):第一次获取对象时才创建对象
饿汉模式(Eager Loading):第一次获取对象时,对象已经创建好。
简洁、高效、不用加锁,但是在某些场景下会有缺陷。
线程安全的单例:
懒汉模式(Lazy Loading):第一次获取对象时才创建对象
class Singleton { public: //获取唯一实例的接口函数 static Singleton* GetInstance() { //双重检查,提高效率,避免高并发场景下每次获取实例对象都进行加锁 if (_sInstance == NULL) { std::lock_guard<std::mutex> lock(_mtx); if (_sInstance == NULL) { Singleton* tmp = new Singleton; MemoryBarrier(); //内存栅栏,防止编译器优化 _sInstance = tmp; } } return _sInstance; } static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { std::cout << _data << std::endl; } private: //构造函数定义为私有,限制只能在类内实例化对象 Singleton() :_data(10) {} //防拷贝 Singleton(const Singleton&); Singleton& operator=(const Singleton&); private: static std::mutex _mtx; //保证线程安全的互斥锁 static Singleton* _sInstance; //指向实例的指针定义为静态私有,这样定义静态成员获取对象实例 int _data; //单例类里面的数据 };
饿汉模式(Eager Loading):第一次获取对象时,对象已经创建好。
简洁、高效、不用加锁,但是在某些场景下会有缺陷。
/*方式一*/ class Singleton { public: static Singleton* GetInstance() { static Singleton sInstance; return &sInstance; } void Print() { std::cout << _data << std::endl; } private: Singleton() :_data(10) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); private: static Singleton* _sInstance; int _data; }; void TestSingleton() { Singleton::GetInstance()->Print(); }
/*方式二*/ class Singleton { public: static Singleton* GetInstance() { static Singleton sInstance; return &sInstance; } static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { std::cout << _data << std::endl; } private: Singleton() :_data(10) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); private: static Singleton* _sInstance; int _data; }; Singleton* Singleton::_sInstance = new Singleton; void TestSingleton() { Singleton::GetInstance()->Print(); Singleton::DelInstance(); }
相关文章推荐
- 设计模式 之 单例模式 (C++ 懒汉经典实现 & DCL实现 & 饿汉经典实现)
- 设计模式C++实现(4)——饿汉懒汉之单例模式
- 单例设计模式(饿汉模式与懒汉模式)
- java的设计模式之单利设计模式(饿汉和懒汉)
- C++的单例模式与线程安全单例模式(懒汉/饿汉)
- 设计模式:单例模式-懒汉模型和饿汉模型
- 设计模式-单例模式懒汉和饿汉的比较
- 设计模式之单例模式:懒汉&饿汉
- C++中的单例模式(懒汉模式、饿汉模式及线程安全问题)
- 单例设计模式-懒汉饿汉
- 老生常谈C++的单例模式与线程安全单例模式(懒汉/饿汉)
- C++的单例模式与线程安全单例模式(懒汉/饿汉)
- C++的单例模式与线程安全单例模式(懒汉/饿汉)
- Java设计模式中单例模式(Design Pattern):懒汉模式和饿汉模式
- C++单例模式(懒汉和饿汉)与线程安全
- Java中的单例设计模式之饿汉模式、懒汉模式
- 设计模式——单例模式之“饿汉模式”与“懒汉模式”
- 多线程-单例设计模式懒汉 饿汉
- C++中的单例模式(懒汉、饿汉)
- Java基础 - 单例(饿汉、懒汉),Runtime类,Timer,线程通信,互斥锁,线程组,线程五种状态,线程池,工厂模式,GUI,适配器设计模式