当前位置: 首页 > 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.yayakq.cn/news/260977/

相关文章:

  • 网站制作公司的流程网站建设中gif
  • 网站建设需要的资料押注网站建设
  • 做网站 不做源码外国媒体网站
  • 太湖手机网站建设建设银行信用卡官网站
  • 榆次网站建设公司wordpress注册代码
  • 无锡网站建设软件开发网站引导插件
  • 高端企业网站源码长寿网站建设公司
  • 网页和网站有什么分别Wordpress图片加载优化
  • mt7620a做网站seo快速上排名
  • 没有做老千的斗牛网站6平面设计专业网站
  • 山东省建设厅制一网站建设工程合同补充协议范本
  • 昆明设计网站建设火车头 wordpress 发布接口
  • 帮人做网站 怎么收费美的网站建设规划书
  • 济南网站建设公司制作中文博客网站模板下载
  • 如何做文献ppt模板下载网站全国全屋定制品牌十大排名
  • 做网站建设优化的电话话术做烘培的网站有哪些
  • wordpress开启全站ssl杭州企业网站建设 哪里好
  • 九江市建设监理有限公司网站网页设计素材
  • 中山网站上排名运城网站推广哪家好
  • 简述建设网站的步骤桂林北站改造最新方案
  • 苏州知名高端网站建设公司如何做好一个购物网站
  • 网站备案密码怎么找回天汇大厦网站建设公司
  • 有什么好的建站公司江苏中小企业建站价格
  • 什么网站可以做全景图判断网站首页
  • 网站分析与优化网站设计怎么做背景颜色
  • 网站建设报价单模板下载网店美工需要掌握哪些软件
  • 住房和城乡建设部官方网站已网站开发哪里接到单子的
  • 品牌网站建设设计公司代备案域名30元
  • 爱淘苗网站开发模式网页设计模板百度云
  • 网站优化员seo招聘电影网站怎么做不犯法