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

做网站国外网站产品线上推广方式都有哪些

做网站国外网站,产品线上推广方式都有哪些,php网站制作 青岛,英文网站建设需要准备什么线程安全 单例模式在单线程中,当然是安全的。但是如果在多线程中,由于并行判断,可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢? 常见的三种方式: 局部静态变量 原理和饿汉模式相似,利用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.shuangfujiaoyu.com/news/57325.html

相关文章:

  • 网站建设电商郑州seo外包平台
  • 上海网站备案公司建站小程序
  • 垂直网站建设的关键因素厦门seo网络推广
  • wordpress空间安装教程视频长沙优化科技有限公司正规吗
  • 网站建设 可行性网站推广在线推广
  • 关于做ppt的网站有哪些内容吗上海今日头条新闻
  • 用自己电脑做网站服务器长沙免费网站建站模板
  • 什么网站可以做ppt网站建设的基本流程
  • 永州网站制作企业网站建站模板
  • 网站为什么改版哪里搜索引擎优化好
  • 赤壁市建设局网站淘宝关键词怎么做排名靠前
  • 网站制作建立手机网站百度关键词排名
  • 国外设计网站 behance入口宁波seo网络推广咨询价格
  • 邢台做移动网站报价百度收录哪些平台比较好
  • 西安网站建设中心镇江seo
  • 网站推广软件下拉管家百度搜索网
  • 网站推广的分类多地优化完善疫情防控措施
  • 郑州网站建设套餐app优化推广
  • 北京南站到北京西站google下载app
  • 浙江住房和建设厅网站网络运营工作内容
  • 做建材哪个网站平台好考试培训
  • 在线做网站视频在线观看网络营销方式对比分析
  • wordpress图片托管seo研究所
  • 新闻网站设计模板合肥新闻 今天 最新消息
  • vr全景网站怎么做策划网络营销活动
  • jing.me wordpress主题西安seo顾问
  • wordpress api接口seo营销工具
  • 做的网站手机打不开怎么办网站快速排名互点软件
  • ubuntu 做网站 分区企业管理培训视频免费
  • 如何在自己的网站上做h5页面抖音seo查询工具