分类

懒汉式:实例对象在第一次被使用时才进行初始化。

饿汉式:实例在定义时就被初始化。

特点

1、构造函数和析构函数私有化,不允许外部创建实例对象。

2、拷贝构造函数和复制运算符重载被delete,不允许产生新的实例。

3、内部定义一个私有的静态数据成员,该成员为本类的实例化对象。

4、提供公有静态成员方法获取该静态对象。

懒汉式

单线程懒汉式实现

 1 class Singleton
2 {
3 public:
4 static Singleton* Ins()
5 {
6 if (_ins == nullptr)
7 _ins = new Singleton();
8 return _ins;
9 }
10
11 protected:
12 Singleton() {}
13 ~Singleton() {}
14 Singleton(const Singleton&) = delete;
15 Singleton& operator=(const Singleton&) = delete;
16
17 private:
18 static Singleton* _ins;
19 };

缺陷:

  • 非线程安全:假设目前有2个线程同时访问Singleton::Ins方法,线程A在if条件判断为真后,失去时间片,此时_ins还未初始化。线程B访问Singleton::Ins,由于在A线程中还未初始化_ins,导致B线程创建对象并将_ins初始化完成。此时,时间片再次回到A线程,还原现场,上一个时间片中已经判断过if条件,_ins将调用new创建新对象实例,导致对象被创建两次,内存泄漏。
  • 内存泄漏:在类中,我们只负责new了一个对象,并未对其delete,导致内存泄漏。

下面将用双重锁+智能指针解决上面缺陷。

双重锁+智能指针实现

 1 class Singleton
2 {
3 public:
4 ~Singleton() { std::cout << "destructor" << std::endl; } //必须声明为public
5
6 static std::shared_ptr<Singleton> Ins()
7 {
8 if (_ins == nullptr)
9 {
10 std::lock_guard<std::mutex> lock(_mt);
11 if (_ins == nullptr)
12 {
13 _ins = std::shared_ptr<Singleton>(new Singleton());
14 }
15 }
16 return _ins;
17 }
18
19 Singleton(const Singleton&) = delete;
20 Singleton& operator=(const Singleton&) = delete;
21
22 protected:
23 Singleton() { std::cout << "constructor" << std::endl; }
24
25 private:
26 static std::shared_ptr<Singleton> _ins;
27 static std::mutex _mt;
28 };

缺点:

  • 双重锁在特定环境下依旧是非线程安全的。
  • 强制用户使用智能指针,并且要求将析构函数定义为piblic,可能在实际使用中忽略了该问题导致程序编译出错。

局部静态变量法(推荐)

 1 class Singleton
2 {
3 public:
4 static Singleton& Ins()
5 {
6 static Singleton _ins;
7 return _ins;
8 }
9
10 Singleton(const Singleton&) = delete;
11 Singleton& operator=(const Singleton&) = delete;
12
13 protected:
14 Singleton() { std::cout << "constructor" << std::endl; }
15 ~Singleton() { std::cout << "destructor" << std::endl; }
16 };

C++11标准中定义了一个Magic Static特性:如果变量当前处于初始化状态,当发生并发访问时,并发线程将会阻塞,等待初始化结束。

std::call_once + 内部类方法

 1 class Singleton
2 {
3 public:
4 static Singleton* Ins()
5 {
6 std::call_once(_flag, []() {
7 _ins = new Singleton;
8 });
9 return _ins;
10 }
11
12 Singleton(const Singleton&) = delete;
13 Singleton& operator=(const Singleton&) = delete;
14
15 protected:
16 Singleton() { std::cout << "constructor" << std::endl; }
17 ~Singleton() { std::cout << "destructor" << std::endl; } //必须声明为私有,否则返回指针将可析构
18
19 private:
20 struct Deleter
21 {
22 ~Deleter() {
23 delete _ins;
24 _ins = nullptr;
25 }
26 };
27 static Deleter _deleter;
28 static Singleton* _ins;
29 static std::once_flag _flag;
30 };
31
32 Singleton::Deleter Singleton::_deleter;
33 Singleton* Singleton::_ins = nullptr;
34 std::once_flag Singleton::_flag;

缺点:不美观,在类内部定义内部类,头文件臃肿,适用于.h和.cpp分离的情况下使用。

饿汉式

 1 class Singleton
2 {
3 public:
4 static Singleton& Ins()
5 {
6 return _ins;
7 }
8
9 Singleton(const Singleton&) = delete;
10 Singleton& operator=(const Singleton&) = delete;
11
12 protected:
13 Singleton() { std::cout << "constructor" << std::endl; }
14 ~Singleton() { std::cout << "destructor" << std::endl; }
15
16 private:
17 static Singleton _ins;
18 };
19
20 Singleton Singleton::_ins;

模板单例

主要解决系统中存在多个模块需要使用单例模式,通过模板+继承解决代码冗余。

 1 template<typename T>
2 class Singleton
3 {
4 public:
5 static T& Ins()
6 {
7 static T _ins;
8 return _ins;
9 }
10
11 protected:
12 Singleton() { std::cout << "constructor" << std::endl; }
13 ~Singleton() { std::cout << "destructor" << std::endl; }
14 Singleton(const Singleton&) = delete;
15 Singleton& operator=(const Singleton&) = delete;
16 };
17
18 class AppInstance : public Singleton<AppInstance>
19 {
20 friend Singleton<AppInstance>; //声明友元
21 AppInstance() {} //必须私有化
22 ~AppInstance() {}
23 public:
24 void func() { std::cout << "func"<< std::endl; }
25 };

总结

除非频繁访问或者频繁创建和销毁,尽量不要使用单例模式,可以用组合的方式代替。

在使用单例模式,需要确保线程安全、内存安全。设计时,尽量做到好用、简单。

c++单例模式总结的更多相关文章

  1. C++实现线程安全的单例模式

    在某些应用环境下面,一个类只允许有一个实例,这就是著名的单例模式.单例模式分为懒汉模式,跟饿汉模式两种. 首先给出饿汉模式的实现 template <class T> class sing ...

  2. 23种设计模式--单例模式-Singleton

    一.单例模式的介绍 单例模式简单说就是掌握系统的至高点,在程序中只实例化一次,这样就是单例模式,在系统比如说你是该系统的登录的第多少人,还有数据库的连接池等地方会使用,单例模式是最简单,最常用的模式之 ...

  3. angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用

    今天我们要讲的是ng2的路由系统. 例子

  4. java设计模式之--单例模式

    前言:最近看完<java多线程编程核心技术>一书后,对第六章的单例模式和多线程这章颇有兴趣,我知道我看完书还是记不住多少的,写篇博客记录自己所学的只是还是很有必要的,学习贵在坚持. 单例模 ...

  5. 设计模式C#合集--单例模式

    单例模式 代码: 第一种: private static Singleton singleton = null; private Singleton() { } public static Singl ...

  6. 设计模式之单例模式(Singleton)

    设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...

  7. GOF23设计模式之单例模式

    ·核心作用: -保证一个类只有一个实例,并且提供一个访问该实例的全局访问点. ·常见应用场景: -Windows的Task Manager(任务管理器)就是很典型的单例模式 -Windows的Recy ...

  8. GJM : C#设计模式(1)——单例模式

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  9. PHP设计模式(四)单例模式(Singleton For PHP)

    今天讲单例设计模式,这种设计模式和工厂模式一样,用的非常非常多,同时单例模式比较容易的一种设计模式. 一.什么是单例设计模式 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对 ...

  10. java设计模式之单例模式(几种写法及比较)

    概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...

随机推荐

  1. autMan奥特曼机器人-对插件权限的管理

    为了避免某些插件在用户不知情的情况下读取使用用户隐私数据,受" 安卓手机上安装的应用需申请电话.位置.通讯录等权限 "的启发,autMan增加了数据桶读取权限设置页面. 当前受限制 ...

  2. 小米9刷twrp Rec 以及写入Magisk详细教程

    首先手机必须先解锁BL锁才能继续: ---------------------- 小米官方BL解锁教程:点此看教程 ---------------------- 解完锁后开始操作: 工具包:点此下载 ...

  3. Kettle - hello world

    Kettle下载页面:点这里 Kettle 版本:pdi-ce-9.4.0.0-343 流程概览 详细步骤 1.首先打开Kettle的Spoon.bat 2.在主对象树中选择转换 3.然后在核心对象的 ...

  4. Sqoop1的导入导出

    Sqoop1 和 Sqoop2 的区别 # 版本上 Sqoop1: 1.4.x Sqoop2: 1.99.x # 架构上 Sqoop1 使用 Sqoop客户端直接提交的方式(命令.将命令封装在脚本中) ...

  5. Elasticsearch7.8搭建(Windows版本单节点、Linux版本单节点、集群,)

    The Elastic Stack, 包括 Elasticsearch.Kibana.Beats 和 Logstash(也称为 ELK Stack).能够安全可靠地获取任何来源.任何格式的数据,然后实 ...

  6. swoole(7)php进程间通信-消息队列

    php实现消息队列操作 ftok:可以将一个路径转换成消息队列可用的key值 msg_get_queue:第一个参数是消息队列的key 第二个参数是消息队列的读写权限 server代码: <?p ...

  7. redmine 迁移后邮箱配置

    https://blog.csdn.net/love8753/article/details/126380927 步骤一 修改配置文件 步骤二 redmine 页面配置信息 步骤一 修改配置文件 打开 ...

  8. PaddleOCR学习笔记3-通用识别服务

    今天优化了下之前的初步识别服务的python代码和html代码. 采用flask + paddleocr+ bootstrap快速搭建OCR识别服务. 代码结构如下: 模板页面代码文件如下: uplo ...

  9. 防止恶意解析——禁止通过IP直接访问网站

    一.什么是恶意解析 一般情况下,要使域名能访问到网站需要两步,第一步,将域名解析到网站所在的主机,第二步,在web服务器中将域名与相应的网站绑定.但是,如果通过主机IP能直接访问某网站,那么把域名解析 ...

  10. 入门Dify平台:知识库分析

    今天,我们来简单了解一下 Dify 的知识库功能.其实,它并没有想象中那么复杂或难以操作.无论是 Dify 还是其他类似平台,都已经将知识库的功能封装得非常完善.你可以直接把它当作一个云端的记事本,内 ...