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

相关文章:

  • 做相册的网站佛山优化网站关键词
  • 阿里云虚拟主机如何上传网站厦门网站建设公司
  • 地方资讯网站源码网站快速收录教程
  • 网站活动怎么做的女教师遭网课入侵直播录屏曝
  • 做盗版网站会怎样100个免费推广b站
  • 哪个浏览器可以看禁止网站找客户资源的软件免费的
  • 贵阳网站建设app开发软文技巧
  • 做网站需要注册的那种百度推广信息流有用吗
  • 商务网站开发与建设制作网页的基本步骤
  • 忘记php网站后台密码seo搜索排名优化是什么意思
  • 万网云虚拟主机上传网站网站网络营销
  • 网站标题改动google网站推广
  • 国家工商登记网河南自助建站seo公司
  • 小程序app定制百度seo如何做
  • 服装网站建设2019网站seo
  • 做网站可以自由职业吗百度seo可能消失
  • 做推广什么网站好域名信息查询
  • 长葛网站建设公司中国军事新闻最新消息
  • 外贸网站建设网站西藏自治区seo 标题 关键词优化
  • 网站建设要多少费用找培训机构的app
  • 徐州网站建设案例最新seo网站优化教程
  • 做网站要不要学ps网页设计模板html代码
  • 网站建设vip教程免费站长工具
  • 网站建设中技术程序今日时政新闻热点
  • 如何做网站直播入口网络营销的策略
  • 上海公安门户网站下载网络营销公司是做什么的
  • 永兴城乡住房建设部网站2024最火的十大新闻有哪些
  • 安徽网站开发公司怎么开网店新手入门
  • 广西省住房和城乡建设厅网站搜外网 seo教程
  • php和织梦那个做网站好知名的网络推广