单例模式
单例模式就是一个类只有一个实例。
为了保证一个类只有一个实例。为了保证一个类只有一个实例,要保证一开始这个类只有唯一一个实例,并且不能进行拷贝等赋值和移动操作。
因此有两种实现方式,第一个是懒汉实现,在需要的时候实例化,另一种是饿汉实现,一开始就实例化。
//懒汉实现 /* **注意C++11以后加入了移动操作,也有了新语法=delete, **但是为了格式整齐,统一让他们权限为private */ #ifndef __SINGLETON1__ #define __SINGLETON1__ class Singleton1 { public: static Singleton1* getInstance(); ~Singleton1() { if(instance) delete instance; } private: Singleton1(){} Singleton1(const Singleton1 &&); Singleton1(const Singleton1 & ); Singleton1& operator=(const Singleton1 &&); Singleton1& operator=(const Singleton1 & ); private: static Singleton1 *instance; }; Singleton1* Singleton1::instance = nullptr; Singleton1* Singleton1::getInstance() { if(instance == nullptr) instance = new Singleton1(); return instance; } #endif // __SINGLETON1__ /*---------------------------------------------*/
//饿汉实现 #ifndef __SINGELTON2__ #define __SINGELTON2__ class Singleton2 { public: static Singleton2* getInstance(); ~Singleton2() { if(instance) delete instance; } private: Singleton2() {} Singleton2(const Singleton2 &&); Singleton2(const Singleton2 & ); Singleton2& operator=(const Singleton2 &&); Singleton2& operator=(const Singleton2 & ); private: static Singleton2* instance; }; Singleton2* Singleton2::instance = new Singleton2(); Singleton2* Singleton2::getInstance(){ return instance; } #endif // !__SINGELTON2__
实例化时如下:
int main() { Singleton1::getInstance();
Singleton2::getInstance(); return 0; }
以上懒汉代码只适合在单线程,懒汉实现在多线程是不安全的,因为有可能两个线程同时运行到实例化初,所以需要加锁,饿汉一直就是线程安全,因为一开始初始化只会保证有一个实例。多线程代码如下:
#ifndef __SINGLETOM_THREAD1__ #define __SINGLETOM_THREAD1__ #include <thread> #include <mutex> //加锁 class Singleton3 { public: static Singleton3* getInstance(); ~Singleton3() { if(instance) delete instance; } private: Singleton3() {}; Singleton3(const Singleton3 &&); Singleton3(const Singleton3 & ); Singleton3 &operator=(const Singleton3 &&); Singleton3 &operator=(const Singleton3 & ); private: static std::mutex mutex_s; static Singleton3* instance; }; std::mutex Singleton3::mutex_s; Singleton3* Singleton3::instance = nullptr; Singleton3* Singleton3::getInstance() { if (instance == nullptr) { mutex_s.lock(); if (instance == nullptr) instance = new Singleton3(); mutex_s.unlock(); } return instance; } #endif // !__SINGLETOM_THREAD1__
【推荐】FlashTable:表单开发界的极速跑车,让你的开发效率一路狂飙
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 于是转身独立开发者
· C#.Net筑基-泛型T & 协变逆变
· dotnet 代码调试方法
· DbContext是如何识别出实体集合的
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· 我救了一个网站,性能提升了1500 多倍!
· 免费开源 .NET OpenCV 迷你运行时全平台发布
· 经验贴!万字总结网卡丢包及ping延迟等网络问题排查思路
· 一个static关键字引发的线上故障:深度剖析静态变量与配置热更新的陷阱
· 10亿订单如何分库分表?