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

做网站国外网站店铺引流的30种方法

做网站国外网站,店铺引流的30种方法,wordpress主题ttfb响应时间,wordpress 可视化建站线程安全 单例模式在单线程中,当然是安全的。但是如果在多线程中,由于并行判断,可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢? 常见的三种方式: 局部静态变量 原理和饿汉模式相似,利用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.15wanjia.com/news/4016.html

相关文章:

  • 哈尔滨建站的系统网站建设杭州
  • 哪些网站做的比较好看的深圳网站开发技术
  • 广告策划公司简介seo网站推广培训
  • 太湖县住房和城乡建设网站市建设局属于免费的网络营销方式
  • 做网站的编程语言产品seo怎么优化
  • 模板网站怎么做百度商家版下载
  • 如何提高网站的搜索排名磁力狗bt
  • 东莞信息网seo关键词有话要多少钱
  • 网站开发工程师月薪seo顾问
  • web到wordpress宁波seo优化
  • 南京电子商务网站开发公司发外链的网址
  • 网络公司网站建设营销方法
  • 建设网站公司不给源代码人工智能培训师
  • 网站制作建立公司品牌推广方案范文
  • 衡阳县专业做淘宝网站域名申请
  • 石家庄做网站的sem投放
  • 做网站在阿里云买什么软件百度指数官网首页
  • 爬墙专用加速器seo优化包括
  • b2b网站建设排名百度官方客服电话
  • 做暧暧的网站18款禁用软件黄app免费
  • 中国室内设计网站官网培训方案模板
  • 建网站流程 知乎怎么做网络广告推广
  • 电子商务网站的开发流程包括推广关键词排名方法
  • 响应式网站如何设计邯郸今日头条最新消息
  • 津南区提供网站建设协议网站流量分析的指标有哪些
  • 网站优化案例分析哪些平台可以免费发布产品
  • 做音乐网站要注意什么外贸网站推广
  • 在合肥做网站前端月薪大概多少百度网盘人工客服电话
  • 做优惠券网站山东东营网络seo
  • 临淄网站制作首选公司百度搜索指数在线查询