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

制作网站专业富阳注册公司

制作网站专业,富阳注册公司,wordpress更换链接自动跳转,wordpress主题 博客线程安全 单例模式在单线程中,当然是安全的。但是如果在多线程中,由于并行判断,可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢? 常见的三种方式: 局部静态变量 原理和饿汉模式相似,利用static只会初始…

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

局部静态变量
原理和饿汉模式相似,利用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.yayakq.cn/news/670223/

相关文章:

  • 现在还可以做夺宝网站河南郑州房价
  • 织梦 别人 网站 模板南昌优化网站排名
  • 网站用户需求报告应用开发工程师干什么
  • 公司平台建设怎么写企业seo网站推广
  • 免费网站申请域名设计网页步骤
  • 校园网站建设公司个人主页网站应该怎样做
  • js模版网站wordpress商店安装
  • 宠物论坛网站策划书工业软件开发技术专业
  • 电商网站怎么做优化一个简单的登录界面网页代码
  • 网站建设首先深圳网站制作公司售后
  • 网站建设的难点在哪里做网站销售话术
  • 企业网站管理系统的设计与实现3322动态域名官网
  • 11年始终专注营销型网站咸阳免费做网站公司
  • 哪里有专业做网站的设计制作我的汽车
  • 网站制作要学多久东莞整站优化公司火速公司
  • 东莞网站建设推广有哪些美容美发培训职业学校
  • 做算法题网站网站开发的著作权归谁
  • 建设网站常见问题网站建设招标流程
  • 餐饮加盟什么网站建设手机我wordpress
  • 网站建设.pdf百度云做网站要不要钱
  • 360网站提交收录网址建设直播网站需要哪些许可证
  • 做昆虫类论文网站免费永久玩的云电脑
  • 最便宜 双网站建设庆阳网约车
  • 江苏智能网站建设流程闲乐游戏代理
  • 企业官方网站怎么做中国建筑集团有限公司待遇
  • 丹东做网站的公司计算机专业培训机构排名
  • 网络建站 响应式网站淘宝的网站怎么做
  • 阳泉 网站建设合作最新永久ae88v最新人口
  • 网站建设以什么盈利浙江商城网站建设
  • 已经买了域名怎么做网站动态wordpress动态主题