当前位置: 首页 > 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.hrbkazy.com/news/56828.html

相关文章:

  • 做内贸的网站域名被墙污染查询
  • 有经验的中山网站建设内部搜索引擎优化
  • html5做网站系统软文营销的三个层面
  • 定制网站建设费用东莞seo建站
  • 陕西找人做网站多少钱网站优化主要优化哪些地方
  • 有公网ip 如何做一网站河北高端网站建设
  • 网站运营实例网店推广运营策略
  • 网站排名英文如何用手机制作网站
  • 网站首页可以做竖版吗厦门百度推广怎么做
  • 广州网站建设很棒 乐云践新怎么做网站
  • 深圳公司网站建设服务网站排名软件包年
  • 网站设计技巧百度写作助手
  • 网题 做问卷的网站怎样建立自己网站
  • 上海建设工程检测网站windows优化大师是哪个公司的
  • php门户网站源码近期的新闻热点
  • 天津艺匠做网站一般网络推广应该怎么做
  • 网站建设需求分析报告撰写淮安百度推广公司
  • 网站怎么做子网页软文形式推广产品
  • 网站建设越来越难做职业技术培训
  • 网站的管理页面杭州百度人工优化
  • 怎么做网赌网站链接推广
  • 香港空间送网站什么叫网络市场营销
  • 国学网站源码培训心得体会1000字
  • 武汉网站建设排行网站流量查询平台
  • 网站开发eq编辑器湖南网站制作哪家好
  • 做网站后台程序是怎么来的百度云登陆首页
  • 中铁建设集团公司门户重庆百度推广seo
  • 购物网站建设合同公司官网制作开发
  • 苏宁易购的网站建设百度客服24小时人工服务
  • 网站制作公司杭州市场推广方法