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

上市公司网站建设方案网站建设系统源码

上市公司网站建设方案,网站建设系统源码,网络营销优化培训,wordpress迁站线程安全 单例模式在单线程中#xff0c;当然是安全的。但是如果在多线程中#xff0c;由于并行判断#xff0c;可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢? 常见的三种方式: 局部静态变量 原理和饿汉模式相似#xff0c;利用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 --stdc11 -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() {     coutmain process id: getpid()endl;     coutmain pthread id: pthread_self()endl;     thread thread1(thr, 1);     thread thread2(thr, 2);     thread1.join();     thread2.join();     return 0; } 结果分析 结果和预想一致饿汉模式在程序编译阶段调用构造函数懒汉模式在调用的时候创建如果不加线程锁会导致创建多个实例。 【C】保证线程安全的单例模式_c 线程安全单例模式-CSDN博客
http://www.w-s-a.com/news/721036/

相关文章:

  • 手机网站被拦截怎么办怎么解决东营建设信息网网
  • 外贸网站模板免费微信网站开发技术
  • 视频盗版网站怎么做福州网站seo
  • 成都金铭 网站建设做网站包含的技术
  • 长沙的网站建设公司哪家好做网站应选那个主题
  • 公司网站百度搜不到如何自己做一个网站
  • 学生如何建设网站网站开发程序
  • 网站建设公司哪家好 皆来磐石网络网站建设"淘宝网" 在颜色选取和搭配方面有哪些值得学习的地方.
  • 网站如何做移动规则适配北京住房与城乡建设部网站
  • 课堂阵地建设网站wordpress运行机制
  • 网站建设的需求方案企业网站建设费用明细
  • 创口贴网站模板京创影视app
  • 团购网站建设目的网站有很多304状态码
  • 运用阿里云怎么做网站外资企业可以在中国境内做网站吗
  • 云南住房和城乡建设局网站西安做官网的公司
  • 企业网站图片上传网站建设和应用的情况
  • 网站不显示内容吗聊城网架公司
  • 南昌网站建设企业网站托管外包怎么做
  • 做非洲外贸的网站网站可以用PS设计吗
  • PHP搭建IDC网站青岛福瀛建设集团网站
  • 安徽网站优化多少钱软件界面设计的基本原则
  • 网站建设动态页面修改删除dnf卖飞机的网站怎么做的
  • 万网是做什么的seo综合
  • 网站关键词分隔符php网站开发平台下载
  • 郑州那家做网站便宜商业计划书免费word版
  • 秦时明月的个人网站怎么做网站开发公司需要招聘哪些人
  • 广告网站建设制作设计服务商安卓app软件定制
  • 公司网站设计与实现中国职业培训在线官方网站
  • 网站服务器空间租用郑州官网网站推广优化
  • 郑州网站建设外包业务wordpress站酷首页