Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Singleton pattern of design pattern: singleton

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1.设计模式是什么?

设计模式其实就是前人总结,代表了最佳实践,对于软件开发过程中对象的封装模式,也是各种复杂问题,极好解耦性的解决方案。

-------------------------------------------------------------------------------------------

下面我们来说一下单例模式的基本概念和代码:

单例类保证了全局只有唯一一个实例对象

单例提供获取这个唯一实例的接口

其实就是保证一个类中出现对象的全局唯一性。

首先对于单例模式而言,有2种

懒汉模式:

#include#includeusing namespace std;class singleton{ public: static singleton* GetInstance() { //使用双重检查,保证获取锁的资源不浪费 if(_instance == NULL) { std::lock_guardlck(_mtx); if(instance == NULL) { //一下解释标记为a singleton *tmp = new singleton(); MemoryBarrier();//内存栅栏。后面会进行解释。 _instance = tmp; } } return _instance; } static void DelInstance() { if(_instance != NULL) { delete _instance; _instance = NULL; } } private: singleton():data(0){}; singleton(const singleton&){}; singleton& operator = (const sigleton&); static singleton* _instance; static mutex _mtx; int data;}singleton* singleton::_instance = NULL;mutex singleton::_mtx;

其实单例模式没有真想象的这么简单,百度上许多说单例模式的文章都说的有些许遗漏

这里主要解释一下内存栅栏的概念

如果我们将a处代码转换为_instance = new singleton();

这在编译器中处理为3个部分,1.分配空间,2调用构造函数,3.赋值、

但是在某些情况话,编译器可能进行优化进行重排,然后顺序变成了1,3,2.将赋值提到了构造函数之前。

然后设想在高并发场景中,_instance已经进行了赋值,但是没有调用构造函数,其他现场进入,直接返回_instance。一个没有调用构造函数的_instance,这就会出现错误

所以我们声明一个临时变量,然后添加一个内存栅栏,其实就是指令顺序的隔断,不可提前。保证赋值构造的完全调用,其实就有点类似Linux中的sigsuspend();

以上就是完善的懒汉模式。

-------------------------------------------------------------------------------------------

饿汉模式:

饿汉模式使用RAII

//1class singleton{ public: static singleton* GetInstance() { static single sInstance; return &sInstance; } private: singleton():data(0){}; singleton(const singleton&){}; singleton& operator = (const sigleton&); static singleton* _instance; int data;}//2class singleton{ public: static sington* GetInstance() { assert(_instance); return _instance; } private: singleton():data(0){}; singleton(const singleton&){}; singleton& operator = (const sigleton&); static singleton* _instance; int data;}singleton* singleton::_instance = new singleton();

以上。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report