当前位置: 首页 > news >正文

单例模式懒汉模式和饿汉模式

线程安全
单例模式在单线程中,当然是安全的。但是如果在多线程中,由于并行判断,可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢?
常见的三种方式:

局部静态变量
原理和饿汉模式相似,利用static只会初始化一次的特性,并且在第一次调用的情况下才会被初始化。推荐使用

class Singleton {
private:
    Singleton() { };
public:
    static Singleton* getInstance() {
        static Singleton *instance = new Singleton();
        return instance;
    }
};

饿汉模式

原理:利用static,在程序编译的时候就调用构造函数实现单例,这样做的优点是保证线程安全,但是缺点就是无论后续是否用到,在编译的时候就会创建,会导致启动性能降低。
实现方法:

class Singleton_Hungry {
public:
    static Singleton_Hungry* getInstance() {
        return singleton;
    }
private:
    Singleton_Hungry() {
        cout << "Hungry creat." << endl;
    }
    static Singleton_Hungry* singleton;
};
Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry();

懒汉模式

原理:利用线程锁,在获取实例的时候判断实例加上线程锁,保证判断的条件只允许一个线程操作。利用锁也可以保证单例只有一个实例。
实现方法:

#include <mutex>
std::mutex mu;
class Singleton_Lazy {
public:
     static Singleton_Lazy* getInstance() {
        if (singleton == NULL) {
            mu.lock();//打开锁
            if (singleton == NULL) {
                singleton = new Singleton_Lazy();
            }
            mu.unlock();//关闭锁
        }
        return singleton;
     }
private:
    Singleton_Lazy() {
        cout << "Lazy creat." << endl;
    }
    static Singleton_Lazy* singleton;
};
Singleton_Lazy* Singleton_Lazy::singleton = NULL;

实践验证

在linux系统上通过命令行g++ single.cpp --std=c++11 -lpthread编译

#include <iostream>
#include <mutex>
#include <thread>
#include <unistd.h>

using namespace std;
mutex mu;

class Singleton_Hungry {
public:
    static Singleton_Hungry* getInstance() {
        return singleton;
    }
private:
    Singleton_Hungry() {
        cout << "Hungry creat." << endl;
    }
    static Singleton_Hungry* singleton;
};
Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry();

class Singleton_Lazy {
private:
    Singleton_Lazy() {
        cout << "Lazy creat." << endl;
    }
    static Singleton_Lazy* singleton;
public:
     static Singleton_Lazy* getInstance() {
        if (singleton == NULL) {
            //mu.lock();//打开锁
            if (singleton == NULL) {
                singleton = new Singleton_Lazy();
            }
            //mu.unlock();//关闭锁
        }
        return singleton;
     }
};
Singleton_Lazy* Singleton_Lazy::singleton = NULL;

void thr(int t) {
    cout << t << " pthread id: " << pthread_self() << endl;
    for(int i = 0; i < 3; i++) {
        Singleton_Lazy *lazy = Singleton_Lazy::getInstance();
        Singleton_Hungry *hungry = Singleton_Hungry::getInstance();
        cout << t << " lazy addr:" << lazy << endl;
        cout << t << " hungry addr:" << hungry << endl;
    }
}

int main() {
    cout<<"main process id: "<<getpid()<<endl;
    cout<<"main pthread id:"<< pthread_self()<<endl;
    thread thread1(thr, 1);
    thread thread2(thr, 2);
    thread1.join();
    thread2.join();
    return 0;
}

结果分析

结果和预想一致,饿汉模式在程序编译阶段调用构造函数,懒汉模式在调用的时候创建,如果不加线程锁会导致创建多个实例。

【C++】保证线程安全的单例模式_c++ 线程安全单例模式-CSDN博客

http://www.lryc.cn/news/405605.html

相关文章:

  • python __repr__和__str__区别
  • huawei USG6001v1学习----NAT和智能选路
  • FPGA JTAG最小系统 EP2C5T144C8N
  • Android 15 之如何快速适配 16K Page Size
  • 学习unity官方的网络插件Netcode【一】
  • QT写一个mainWindow
  • Java查找算法练习(2024.7.23)
  • 洗地机哪个牌子好?四款口碑最好的洗地机排名推荐
  • 如何提升短视频的曝光量和获客效能?云微客来解决
  • SpringBoot开发中如何缓存数据, 减少数据库的访问频率?
  • PostgreSQL如何在windows/linux开启归档
  • 【启明智显分享】基于国产Model3芯片的7寸触摸屏助力智慧医疗,电子床头屏提升护理交互
  • 从理论到实践:如何用 TDengine 打造完美数据模型​
  • 可以免费合并pdf的软件 合并pdf文件的软件免费 合并pdf的软件免费
  • 【排序 滑动窗口 】1498. 满足条件的子序列数目
  • RabbitMQ普通集群搭建指南
  • AGV平面坐标系变换公式及实例
  • es切片和集群
  • IEEE官方列表会议 | 第三届能源与环境工程国际会议(CFEEE 2024)
  • 深度学习中的正则化技术 - Dropout篇
  • 《昇思 25 天学习打卡营第 18 天 | 扩散模型(Diffusion Models) 》
  • 【Django+Vue3 线上教育平台项目实战】Elasticsearch实战指南:从基础到构建课程搜索与数据同步接口
  • libtins初探-抓包嗅探
  • 大语言模型-Bert-Bidirectional Encoder Representation from Transformers
  • bug诞生记——动态库加载错乱导致程序执行异常
  • Matlab演示三维坐标系旋转
  • redis的持久化机制以及集群模式
  • 【论文解读】大模型算法发展
  • WebApi配置Swagger、Serilog、NewtonsoftJson、Sqlsugar、依赖注入框架Autofac、MD5加密
  • 【ffmpeg命令基础】视频选项讲解