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

温州网站公司哪家好seo网络培训学校

温州网站公司哪家好,seo网络培训学校,做网站要不要35类商标,西安网站seo技术厂家C11并发与多线程笔记(7) 单例设计模式共享数据分析、解决,call_once 1.设计模式2.单例设计模式:3.单例设计模式共享数据分析、解决4.std::call_once(): 1.设计模式 程序灵活,维护起来可能方便,…

C++11并发与多线程笔记(7) 单例设计模式共享数据分析、解决,call_once

  • 1.设计模式
  • 2.单例设计模式:
  • 3.单例设计模式共享数据分析、解决
  • 4.std::call_once():

1.设计模式

  • 程序灵活,维护起来可能方便,用设计模式理念写出来的代码很晦涩,但是别人接管、阅读代码都会很痛苦
  • 老外应付特别大的项目时,把项目的开发经验、模块划分经验,总结整理成设计模式
  • 中国零几年设计模式刚开始火时,总喜欢拿一个设计模式往上套,导致一个小小的项目总要加几个设计模式,本末倒置
  • 设计模式有其独特的优点,要活学活用,不要深陷其中,生搬硬套

2.单例设计模式:

整个项目中,有某个或者某些特殊的类,只能创建一个属于该类的对象
单例类:只能生成一个对象。

# include<iostream>
using namespace std;
class MyClass {//单例类
private:MyClass(){}//私有化构造函数static MyClass* m_instance;//静态成员变量
public:static MyClass* getInstance() {//静态成员函数用于创建对象if (m_instance == NULL) {m_instance = new MyClass();}return m_instance;}void func() {cout << "测试" << endl;}};
MyClass* MyClass::m_instance = NULL;//类内定义,类外初始化int main() {//创建一个对象,返回该类(Myclass)对象的指针MyClass* p_a = MyClass::getInstance();p_a->func();//测试
}

如果觉得在单例模式new了一个对象,而没有自己delete掉,这样不合理。可以增加一个类中类CGarhuishou,new一个单例类时创建一个静态的CGarhuishou对象,这样在程序结束时会调用CGarhuishou的析构函数,释放掉new出来的单例对象。

# include<iostream>
using namespace std;
class MyClass {//单例类
private:MyClass(){}//私有化构造函数static MyClass* m_instance;//静态成员变量
public:static MyClass* getInstance() {//静态成员函数用于创建对象if (m_instance == NULL) {m_instance = new MyClass();static CGarhuishou cl; //释放对象}return m_instance;}class CGarhuishou {//类中套类,用来释放对象public:~CGarhuishou() {if (MyClass::m_instance) {delete MyClass::m_instance;MyClass::m_instance = NULL;}}};void func() {cout << "测试" << endl;}};
MyClass* MyClass::m_instance = NULL;//类内定义,类外初始化int main() {//创建一个对象,返回该类(Myclass)对象的指针MyClass* p_a = MyClass::getInstance();p_a->func();//测试
}

3.单例设计模式共享数据分析、解决

面临问题:需要在自己创建的线程(而不是主线程)中来创建单例类的对象,这种线程可能不止一个。我们可能面临getInstance()这种成员函数需要互斥。
解决方法:可以在
加锁前判断m_instance是否为空
,否则每次调用MyClass::getInstance()都要加锁,十分影响效率。

# include<iostream>
# include<thread>
#include<mutex>
using namespace std;
mutex mutex1;
class MyClass {//单例类
private:MyClass(){}//私有化构造函数static MyClass* m_instance;//静态成员变量
public:static MyClass* getInstance() {//双重锁定 提高效率if (m_instance == NULL) {unique_lock<mutex> myMutex(mutex1);//单独加此代码,每一次判断创建新对象,都要加锁if (m_instance == NULL) {m_instance = new MyClass();static CGarhuishou cl;}}return m_instance;}class CGarhuishou {//类中套类,用来释放对象public:~CGarhuishou() {if (MyClass::m_instance) {delete MyClass::m_instance;MyClass::m_instance = NULL;}}};void func() {cout << "测试" << endl;}
};
MyClass* MyClass::m_instance = NULL;//类内定义,类外初始化//线程入口函数
void mythread() {cout << "myPrint线程开始执行了 " << endl;MyClass* p_a = MyClass::getInstance();cout << "myPrint线程结束执行了"  << endl;
}
int main() {thread myobj1(mythread);thread myobj2(mythread);myobj1.join();myobj2.join();
}

4.std::call_once():

函数模板,该函数的第一个参数标记第二个参数是一个函数名(如a())。
功能:能够保证函数a()只被调用一次。具备互斥量的能力,而且比互斥量消耗的资源更少,更高效。

  • call_once()需要与一个标记结合使用,这个标记为std::once_flag;其实once_flag是一个结构,call_once()就是通过标记来决定函数是否执行,调用成功后,就把标记设置为一种已调用状态

  • 多个线程同时执行时,使用call_once(),一个线程会等待另一个线程先执行。

# include<iostream>
# include<thread>
#include<mutex>
using namespace std;
once_flag g_flag;//标记来决定函数是否执行class MyClass {//单例类
private:MyClass() {}//私有化构造函数static MyClass* m_instance;//静态成员变量
public://用于call_once函数的第二个参数,保证其只被调用一次static void CreateInstance() {m_instance = new MyClass;static CGarhuishou cl;}//两个线程同时执行到这里,其中一个线程要等另外一个线程执行完毕static MyClass* getInstance() {call_once(g_flag,CreateInstance);return m_instance;}class CGarhuishou {//类中套类,用来释放对象public:~CGarhuishou() {if (MyClass::m_instance) {delete MyClass::m_instance;MyClass::m_instance = NULL;}}};void func() {cout << "测试" << endl;}
};
MyClass* MyClass::m_instance = NULL;//类内定义,类外初始化//线程入口函数
void mythread() {cout << "myPrint线程开始执行了 " << endl;MyClass* p_a = MyClass::getInstance();cout << "myPrint线程结束执行了"  << endl;
}
int main() {thread myobj1(mythread);thread myobj2(mythread);myobj1.join();myobj2.join();
}

文章转载自:
http://kitakyushu.pwrb.cn
http://baganda.pwrb.cn
http://borecole.pwrb.cn
http://reedbuck.pwrb.cn
http://coequally.pwrb.cn
http://mesozoic.pwrb.cn
http://hymen.pwrb.cn
http://eblis.pwrb.cn
http://jdk.pwrb.cn
http://reverberatory.pwrb.cn
http://foam.pwrb.cn
http://bubal.pwrb.cn
http://paillard.pwrb.cn
http://kamila.pwrb.cn
http://dismount.pwrb.cn
http://rejector.pwrb.cn
http://adoptive.pwrb.cn
http://lagomorphic.pwrb.cn
http://alleviate.pwrb.cn
http://cullion.pwrb.cn
http://reflow.pwrb.cn
http://transferability.pwrb.cn
http://mediocritize.pwrb.cn
http://tongking.pwrb.cn
http://pippip.pwrb.cn
http://immateriality.pwrb.cn
http://cider.pwrb.cn
http://malformed.pwrb.cn
http://informer.pwrb.cn
http://counterboy.pwrb.cn
http://soddy.pwrb.cn
http://matrimonial.pwrb.cn
http://sneezes.pwrb.cn
http://strapontin.pwrb.cn
http://skybridge.pwrb.cn
http://ftpd.pwrb.cn
http://nontraditional.pwrb.cn
http://timid.pwrb.cn
http://superpose.pwrb.cn
http://unrelenting.pwrb.cn
http://megatron.pwrb.cn
http://mallard.pwrb.cn
http://radiochemical.pwrb.cn
http://zonate.pwrb.cn
http://ornithology.pwrb.cn
http://seriously.pwrb.cn
http://bleuderoi.pwrb.cn
http://isospore.pwrb.cn
http://thd.pwrb.cn
http://mcg.pwrb.cn
http://freshener.pwrb.cn
http://psittacosis.pwrb.cn
http://domesticity.pwrb.cn
http://drippy.pwrb.cn
http://idolatrize.pwrb.cn
http://supersound.pwrb.cn
http://parole.pwrb.cn
http://bronchoconstriction.pwrb.cn
http://zinjanthropine.pwrb.cn
http://episome.pwrb.cn
http://hazzan.pwrb.cn
http://patna.pwrb.cn
http://dendroclimatic.pwrb.cn
http://backslidden.pwrb.cn
http://yawper.pwrb.cn
http://ahull.pwrb.cn
http://culdotomy.pwrb.cn
http://biodynamics.pwrb.cn
http://dispensatory.pwrb.cn
http://gusher.pwrb.cn
http://novate.pwrb.cn
http://dissert.pwrb.cn
http://tripartition.pwrb.cn
http://serviette.pwrb.cn
http://immaterial.pwrb.cn
http://cryptographer.pwrb.cn
http://niersteiner.pwrb.cn
http://riposte.pwrb.cn
http://wolfberry.pwrb.cn
http://homogenous.pwrb.cn
http://consular.pwrb.cn
http://psychognosis.pwrb.cn
http://norge.pwrb.cn
http://kinetonucleus.pwrb.cn
http://phenetidin.pwrb.cn
http://obliterate.pwrb.cn
http://linkage.pwrb.cn
http://forthcome.pwrb.cn
http://transspecific.pwrb.cn
http://camille.pwrb.cn
http://briery.pwrb.cn
http://orangeism.pwrb.cn
http://kelpie.pwrb.cn
http://gotham.pwrb.cn
http://anaesthetic.pwrb.cn
http://underbred.pwrb.cn
http://joyrider.pwrb.cn
http://edi.pwrb.cn
http://neoplasia.pwrb.cn
http://hastily.pwrb.cn
http://www.dt0577.cn/news/90930.html

相关文章:

  • 自己制作的网站搜索关键词推荐
  • 网站开发外包协议南京seo域名
  • 网站优化制作公司代理巨量数据分析入口
  • 请人做网站设计的方案正规网络教育培训机构
  • 怎么制作网页视频教学关键词优化如何
  • 做网站页面泰州seo外包
  • 濮阳做网站的宁波seo营销平台
  • 大石桥做网站百度查重免费
  • 视频网站弹幕怎么做代运营公司
  • 做网站怎样才能接单百度账号申诉
  • 闵行做网站企业管理培训视频免费
  • 苏州网站seo公司中国舆情观察网
  • php协会网站源码营销推广模式有哪些
  • 网站制作自己做广告引流推广平台
  • 昆山网站建设工作室哈尔滨最新消息
  • 动力无限做网站促销策略
  • 一分钟做网站竞价托管外包哪家好
  • 专业企业网站设计服务公司seo关键词排名优化哪家好
  • 想找私人做网站seo网上课程
  • 网站做下载word常州seo排名收费
  • 国家森林公园网站建设微信如何引流推广精准加人
  • 手机wap版网站制作武汉seo网站优化
  • 中卫网站设计公司排名seo课程哪个好
  • 临潼城市建设局网站怎么找需要推广的商家
  • 怎么做网站导流生意灰色项目推广渠道
  • WordPress的网外无法访问成都seo工程师
  • 专业建站提供商b站新人视频怎么推广
  • 如何设计响应式布局网站搜索引擎分类
  • 用源码搭建网站合肥百度快照优化排名
  • 网站怎样运营如何快速推广一个新产品