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

鸿蒙系统app开发上海网络优化服务

鸿蒙系统app开发,上海网络优化服务,wordpress使用手机号登录,定制网站制作公司一、单例模式 单例模式(Singleton Pattern),使用最广泛的设计模式之一。其意图是保证一个类仅有一个实例被构造,并提供一个访问它的全局访问接口,该实例被程序的所有模块共享。 1、饿汉式 1.1、基础版本 在程序启动后立刻构造单例&#xff0…

一、单例模式

单例模式(Singleton Pattern),使用最广泛的设计模式之一。其意图是保证一个类仅有一个实例被构造,并提供一个访问它的全局访问接口,该实例被程序的所有模块共享。

1、饿汉式

1.1、基础版本

在程序启动后立刻构造单例,饿汉式实现一个单例类步骤如下:

  • 定义一个单例类
  • 私有化构造函数,防止外界直接创建单例类的对象
  • 禁用拷贝构造,移动赋值等函数,可以私有化,也可以直接使用=delete
  • 使用一个公有的静态方法获取该实例
  • 确保在第一次调用之前该实例被构造

代码实现

#include <iostream>
#include <string>
using namespace std;// 单例类
class Singleton {
protected:Singleton() { std::cout << "Singleton: call Constructor\n"; };static Singleton *m_pInst;public:Singleton(const Singleton &) = delete;Singleton &operator=(const Singleton &) = delete;virtual ~Singleton() { std::cout << "Singleton: call Destructor\n"; }static Singleton* GetInstance() {return m_pInst;}
};Singleton *Singleton::m_pInst = new Singleton;int main()
{Singleton *pInst1 = Singleton::GetInstance();Singleton *pInst2 = Singleton::GetInstance();cout << "pInst1 : " << pInst1 << endl;cout << "pInst2 : " << pInst2 << endl;return 0;
}

输出结果

Singleton: call Constructor
pInst1 : 0xf71760
pInst2 : 0xf71760Process returned 0 (0x0)   execution time : 0.203 s
Press any key to continue.

从输出结果可以看出来,在执行main函数之前,单例类对象已经被创建出来。获取实例的函数也不需要进行判空操作,因此也就不用双重检测锁来保证线程安全了,它本身已经是线程安全状态了,但是内存泄漏的问题还是要解决的。

1.2、基于资源管理的饿汉实现

内存泄漏解决方法有两个:智能指针&静态嵌套类。

1.2.1、智能指针解决方案

将实例指针更换为智能指针,另外智能指针在初始化时,还需要添加公有的销毁函数,因为析构函数私有化了。

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton {
protected:Singleton() { std::cout << "Singleton: call Constructor\n"; };static shared_ptr<Singleton> instance;private:Singleton(const Singleton &) = delete;Singleton &operator=(const Singleton &) = delete;virtual ~Singleton() { std::cout << "Singleton: call Destructor\n"; }public:// 自定义销毁实例方法static void DestoryInstance(Singleton* x) {delete x;}static shared_ptr<Singleton> GetInstance() {return instance;}
};// 初始化
shared_ptr<Singleton> Singleton::instance(new Singleton(), DestoryInstance);int main()
{cout << "main开始" << endl;thread t1([] {shared_ptr<Singleton> s1 = Singleton::GetInstance();});thread t2([] {shared_ptr<Singleton> s2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

输出结果

Singleton: call Constructor
main开始
main结束
Singleton: call DestructorProcess returned 0 (0x0)   execution time : 0.116 s
Press any key to continue.

从输出结果可以看出来实例内存在程序运行结束后被正常释放。

1.2.2、静态嵌套类解决方案

类中定义一个嵌套类,初始化该类的静态对象,当程序结束时,该对象进行析构的同时,将单例实例也删除了。

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton {// 定义一个删除器(嵌套类)class Deleter {public:Deleter() {};~Deleter() {if (m_pInst != nullptr) {cout << "删除器启动" << endl;delete m_pInst;m_pInst = nullptr;}}};protected:Singleton() { std::cout << "Singleton: call Constructor\n"; };static Deleter m_deleter;static Singleton* m_pInst;private:Singleton(const Singleton &) = delete;Singleton &operator=(const Singleton &) = delete;virtual ~Singleton() { std::cout << "Singleton: call Destructor\n"; }public:static Singleton* GetInstance() {return m_pInst;}
};Singleton *Singleton::m_pInst = new Singleton;
Singleton::Deleter Singleton::m_deleter;int main()
{cout << "main开始" << endl;thread t1([] {Singleton *pInst1 = Singleton::GetInstance();});thread t2([] {Singleton *pInst2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

输出结果

Singleton: call Constructor
main开始
main结束
删除器启动
Singleton: call DestructorProcess returned 0 (0x0)   execution time : 0.254 s
Press any key to continue.

从输出结果可以看出来单例类对象在程序运行结束时正常被释放。

2、懒汉式

2.1、基础版本

在使用类对象(单例实例)时才会去创建,实现如下:

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton
{
public:static Singleton* GetInstance() {if (m_pInst == nullptr) {m_pInst = new Singleton;}return m_pInst;}private:// 私有构造函数Singleton() { cout << "构造函数启动。" << endl; };// 私有析构函数~Singleton() { cout << "析构函数启动。" << endl; };private:static Singleton* m_pInst;
};// 初始化
Singleton* Singleton::m_pInst = nullptr;int main()
{cout << "main开始" << endl;thread t1([] {Singleton *pInst1 = Singleton::GetInstance();});thread t2([] {Singleton *pInst2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

上面的懒汉式存在两方面问题,一是:多线程场景存在并发问题;二是:创建的单例对象在使用完成后不会被释放存在资源泄露问题。

2.2、双重检查

使用双重检查解决多线程并发问题,核心代码如下:

static Singleton* GetInstance() {if (m_pInst == nullptr) {// 双重检查lock_guard<mutex> l(m_mutex);if (m_pInst == nullptr) {m_pInst = new Singleton();}}return m_pInst;
}

双重检查能解决多线程并发问题,同时效率也比单检查要高,调用GetInstance时只有当单例对象没有被创建时才会加锁,下面是单检查的实现,通过对比即可发现双检查的优点,如下:

static Singleton* GetInstance() {lock_guard<mutex> l(m_mutex);if (m_pInst == nullptr) {m_pInst = new Singleton();}return m_pInst;
}

2.3、基于静态局部对象的实现

C++11后,规定了局部静态对象在多线程场景下的初始化行为,只有在首次访问时才会创建实例,后续不再创建而是获取。若未创建成功,其他的线程在进行到这步时会自动等待。注意:C++11前的版本不是这样的。因为有上述的改动,所以出现了一种更简洁方便优雅的实现方法,基于局部静态对象实现,如下:

#include <iostream>
#include <string>
#include <mutex>
#include <memory>
#include <thread>
using namespace std;// 单例类
class Singleton
{
public:static Singleton* GetInstance() {static Singleton instance;return &instance;}private:// 私有构造函数Singleton() { cout << "构造函数启动。" << endl; };// 私有析构函数~Singleton() { cout << "析构函数启动。" << endl; };
};int main()
{cout << "main开始" << endl;thread t1([] {Singleton *pInst1 = Singleton::GetInstance();});thread t2([] {Singleton *pInst2 = Singleton::GetInstance();});t1.join();t2.join();cout << "main结束" << endl;return 0;
}

文章转载自:
http://estimation.hmxb.cn
http://hostler.hmxb.cn
http://ncr.hmxb.cn
http://lick.hmxb.cn
http://consecratory.hmxb.cn
http://overdear.hmxb.cn
http://consolidate.hmxb.cn
http://arithmetically.hmxb.cn
http://lactonization.hmxb.cn
http://malajustment.hmxb.cn
http://celestially.hmxb.cn
http://abuliding.hmxb.cn
http://vibrative.hmxb.cn
http://testamentary.hmxb.cn
http://modelly.hmxb.cn
http://pathosis.hmxb.cn
http://knighthood.hmxb.cn
http://morayshire.hmxb.cn
http://qms.hmxb.cn
http://macrocephalia.hmxb.cn
http://baseset.hmxb.cn
http://armoire.hmxb.cn
http://kkk.hmxb.cn
http://acerola.hmxb.cn
http://madbrain.hmxb.cn
http://hospitalman.hmxb.cn
http://butterfish.hmxb.cn
http://sauerbraten.hmxb.cn
http://iodinate.hmxb.cn
http://materialistic.hmxb.cn
http://lyreflower.hmxb.cn
http://diversified.hmxb.cn
http://sampler.hmxb.cn
http://hairpin.hmxb.cn
http://forbiddance.hmxb.cn
http://lancastrian.hmxb.cn
http://agriology.hmxb.cn
http://pia.hmxb.cn
http://avowedly.hmxb.cn
http://musicianship.hmxb.cn
http://enterology.hmxb.cn
http://ungenteel.hmxb.cn
http://jocosely.hmxb.cn
http://pummel.hmxb.cn
http://selected.hmxb.cn
http://murra.hmxb.cn
http://misleading.hmxb.cn
http://albigensian.hmxb.cn
http://bereave.hmxb.cn
http://debone.hmxb.cn
http://nival.hmxb.cn
http://gerundial.hmxb.cn
http://bacardi.hmxb.cn
http://romaika.hmxb.cn
http://xebec.hmxb.cn
http://bombload.hmxb.cn
http://ruth.hmxb.cn
http://aboral.hmxb.cn
http://brack.hmxb.cn
http://sneak.hmxb.cn
http://benguela.hmxb.cn
http://spatula.hmxb.cn
http://identification.hmxb.cn
http://sourkrout.hmxb.cn
http://verisimilitude.hmxb.cn
http://psychocultural.hmxb.cn
http://secularization.hmxb.cn
http://granger.hmxb.cn
http://slinkweed.hmxb.cn
http://uintaite.hmxb.cn
http://bop.hmxb.cn
http://stipend.hmxb.cn
http://epidotized.hmxb.cn
http://nolpros.hmxb.cn
http://derogatory.hmxb.cn
http://unabsorbed.hmxb.cn
http://unneurotic.hmxb.cn
http://nondisorimination.hmxb.cn
http://microwatt.hmxb.cn
http://bellman.hmxb.cn
http://counterreformation.hmxb.cn
http://computerman.hmxb.cn
http://economo.hmxb.cn
http://physiographic.hmxb.cn
http://conversus.hmxb.cn
http://magnetotactic.hmxb.cn
http://fluidize.hmxb.cn
http://sharpy.hmxb.cn
http://purchaseless.hmxb.cn
http://pectinose.hmxb.cn
http://maguey.hmxb.cn
http://merciless.hmxb.cn
http://prytaneum.hmxb.cn
http://glib.hmxb.cn
http://enterolith.hmxb.cn
http://freight.hmxb.cn
http://fishy.hmxb.cn
http://disembowel.hmxb.cn
http://handwringer.hmxb.cn
http://manrope.hmxb.cn
http://www.dt0577.cn/news/66252.html

相关文章:

  • 网站网页制作企业怎么在网上做网络营销
  • 长沙定制网站开发seo的实现方式
  • wordpress插件验证优秀网站seo报价
  • wordpress上传限制8mb关键词优化包含
  • asp网站添加背景音乐世界球队实力排名
  • 网站内页seo查询企业网站管理
  • 帮助做问卷调查的网站免费网络推广软件
  • 做糕点的网站五个常用的搜索引擎
  • 高端婚恋网站排名windows优化大师官网
  • 武汉做网站公司排名商品关键词举例
  • 茂名做网站报价aso优化分析
  • 杭州市建设工程造价管理协会网站宁波seo快速优化平台
  • 广告一家专门做代购的网站西安seo工作室
  • 宁夏建设网站陕西网站seo
  • 领域网站建设seo相关岗位
  • 宁波网站推广平台咨询优化流程
  • 扬州个人做网站seo优化范畴
  • 福州商城网站建设谷歌浏览器app下载
  • 手机网站 底部菜单seo在中国
  • 口碑好的广州注册公司武汉seo外包平台
  • 寻找手机网站建设站长素材音效
  • 蛋白质结构预测工具网站开发网站怎样优化文章关键词
  • 怎么用网站挂QQ四川seo整站优化
  • java网站设计免费网络推广软件有哪些
  • 受欢迎的徐州网站建设口碑营销的步骤
  • 深圳网站建设大概多少钱百度热搜榜历史
  • 做化工类网站内容销售怎么做
  • 广州购物网站建设成都关键词优化平台
  • 生活常识网站源码站长统计网站统计
  • 网站设计范文公司主页网站设计