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

做网站国外网站大连网站建设

做网站国外网站,大连网站建设,软件开发主要文档,香港网站做购物商城会罚款吗线程安全 单例模式在单线程中,当然是安全的。但是如果在多线程中,由于并行判断,可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢? 常见的三种方式: 局部静态变量 原理和饿汉模式相似,利用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.dt0577.cn/news/18936.html

相关文章:

  • 外贸网站建设需求安卓神级系统优化工具
  • 滁州做网站优化网络seo优化推广
  • 做的最好的美女视频网站有哪些提高百度快速排名
  • 湖南城乡建设厅官方网站qq群推广链接
  • 湖南疫情浏阳最新消息枫树seo
  • 南京做南京华美整容网站百度搜索引擎收录
  • 网络销售网站外包app拉新佣金排行榜
  • 怎样在手机做自己的网站6网站域名综合查询
  • 网站商品图片怎么做网站推广计划书
  • 用bootstrap3做的网站国外友链买卖平台
  • 南京医疗网站建设网络营销策划需要包括哪些内容
  • 兼职做任务的网站网站收录批量查询
  • 网站开发语言识别山西搜索引擎优化
  • 专门做网站推广的平台东莞关键词seo优化
  • seo整站优化费用网络营销师报名入口
  • layui框架的wordpress引擎优化seo是什么
  • 前端做网站直播如何检测网站是否安全
  • 怎样制作表白网站如何自己做一个网页
  • 中国建设厅官方网站网络营销的未来发展趋势论文
  • 住建部关于epc总承包文件seo推广公司有哪些
  • 免费行情软件网站大全下载搜索引擎排名的三大指标
  • 怎么查网站做404页面没全网营销是什么意思
  • 长沙网站建设哪个好郴州网站建设
  • 上海外贸建站帮平台做推广怎么赚钱
  • 给客户做网站建设方案重庆镇海seo整站优化价格
  • 门户网站域名是什么关键词歌词图片
  • 采招网是怎样做的网站网络营销讲师
  • 注册型网站推广视频优化软件
  • 做体力活的网站网站里的友情链接
  • 网站备案必须在公司注册地重庆seo排名方法