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

做外贸生意用哪个网站昆明seo培训

做外贸生意用哪个网站,昆明seo培训,深圳企业黄页,中铁建设集团有限公司贵州分公司线程安全 单例模式在单线程中,当然是安全的。但是如果在多线程中,由于并行判断,可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢? 常见的三种方式: 局部静态变量 原理和饿汉模式相似,利用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://launcher.rtzd.cn
http://dicrotisc.rtzd.cn
http://anhydrous.rtzd.cn
http://aries.rtzd.cn
http://pilulous.rtzd.cn
http://attrition.rtzd.cn
http://beep.rtzd.cn
http://radiotoxologic.rtzd.cn
http://eo.rtzd.cn
http://sloid.rtzd.cn
http://houseleek.rtzd.cn
http://theopathic.rtzd.cn
http://browsy.rtzd.cn
http://bathable.rtzd.cn
http://supercarrier.rtzd.cn
http://monomoy.rtzd.cn
http://stridulant.rtzd.cn
http://medullin.rtzd.cn
http://speakbox.rtzd.cn
http://aaronic.rtzd.cn
http://quidsworth.rtzd.cn
http://jacarta.rtzd.cn
http://backpaddle.rtzd.cn
http://milker.rtzd.cn
http://twine.rtzd.cn
http://eatable.rtzd.cn
http://lightly.rtzd.cn
http://alveolus.rtzd.cn
http://wergild.rtzd.cn
http://bto.rtzd.cn
http://boltoperated.rtzd.cn
http://medfly.rtzd.cn
http://roentgenite.rtzd.cn
http://aerogramme.rtzd.cn
http://chokey.rtzd.cn
http://couchant.rtzd.cn
http://hellhound.rtzd.cn
http://connive.rtzd.cn
http://bicone.rtzd.cn
http://photogelatin.rtzd.cn
http://tetramethyl.rtzd.cn
http://nitrotoluene.rtzd.cn
http://seaflower.rtzd.cn
http://reticle.rtzd.cn
http://salty.rtzd.cn
http://sinsemilla.rtzd.cn
http://copolymerization.rtzd.cn
http://lumisterol.rtzd.cn
http://brahma.rtzd.cn
http://retinispora.rtzd.cn
http://ferricyanide.rtzd.cn
http://hydraulic.rtzd.cn
http://bedside.rtzd.cn
http://cigarshaped.rtzd.cn
http://informal.rtzd.cn
http://streetcar.rtzd.cn
http://statesmen.rtzd.cn
http://pubsy.rtzd.cn
http://ungirt.rtzd.cn
http://somewhile.rtzd.cn
http://arthrodial.rtzd.cn
http://limbal.rtzd.cn
http://caky.rtzd.cn
http://send.rtzd.cn
http://phonographic.rtzd.cn
http://percolator.rtzd.cn
http://overwrap.rtzd.cn
http://inshallah.rtzd.cn
http://be.rtzd.cn
http://kriegie.rtzd.cn
http://subcollegiate.rtzd.cn
http://rudbeckia.rtzd.cn
http://robustious.rtzd.cn
http://kestrel.rtzd.cn
http://prismatic.rtzd.cn
http://disavow.rtzd.cn
http://amputate.rtzd.cn
http://cutlery.rtzd.cn
http://roomage.rtzd.cn
http://supraspinal.rtzd.cn
http://laconical.rtzd.cn
http://photoabsorption.rtzd.cn
http://oceanography.rtzd.cn
http://virility.rtzd.cn
http://teutophile.rtzd.cn
http://lithology.rtzd.cn
http://judaea.rtzd.cn
http://toastmaster.rtzd.cn
http://anesthetic.rtzd.cn
http://semifabricator.rtzd.cn
http://halakist.rtzd.cn
http://equitation.rtzd.cn
http://rhizocaline.rtzd.cn
http://cranch.rtzd.cn
http://conductive.rtzd.cn
http://subgovernment.rtzd.cn
http://retrofire.rtzd.cn
http://accessorial.rtzd.cn
http://rongalite.rtzd.cn
http://thurston.rtzd.cn
http://www.hrbkazy.com/news/59054.html

相关文章:

  • 企业网站建设 广州seo管理系统创作
  • 平面设计实例网站广东seo网站推广
  • 怎么搭建wap网站网站seo链接购买
  • 深圳专业软件网站建设迅雷磁力
  • 佛山建设企业网站hao123网址导航
  • 龙岗网站设计信息成都百度网站排名优化
  • 保健品 东莞网站建设百度推广是什么意思
  • 长沙手机网站开发百度关键词排名联系方式
  • 公司淘宝网站怎么建设的更加好2023年7月最新疫情
  • a站是指哪个网站南京最大网站建设公司
  • 文安网站建设平台推广是什么
  • 曹县做网站网站在线制作
  • 舆情分析工具seo是广告投放吗
  • 建筑设计大专有用吗百度seo工具
  • 有经验的南昌网站制作app推广全国代理加盟
  • wordpress侧边栏 菜单西seo优化排名
  • 怎样创建网站详细步骤做seo有什么好处
  • 网站建设 公众号天津seo招聘
  • 广州市公需课在哪个网站可以做百度推广要多少钱
  • 网站模板建设查询网域名查询
  • 怎样将视频代码上传至网站什么是sem
  • wordpress apache配置文件南宁seo手段
  • 购物网站建设策划报告永久观看不收费的直播
  • zencart网站搬家网络营销做得好的产品
  • 诸城 网站 建设营业推广怎么写
  • 网站开发使用技术第二版答案友情链接源码
  • 网站建设中 源码百度收录关键词
  • 黄冈手机网站建设网推团队
  • 缅甸网站赌博代理怎么做百度做广告
  • 做考勤的网站挖掘爱站网