首页 > 作文

C++单例模式的懒汉模式和饿汉模式详解

更新时间:2023-04-06 03:35:33 阅读: 评论:0

懒汉模式

懒汉模式在第一次用到类实例的时候才会去实例化,就是不到调用getinstance函数时,这个类的对象是一直不存在的。懒汉本身是线程不安全的

#include <iostream>using namespace std;class singelton{private:    singelton(){        m_count ++;        printf("singelton begin\n");        sleep(1000);// 加sleep为了放大效果        printf("singelton end\n");    }    static singelton *single;//定义一个唯一指向实例的指针,并且是私有的public:    static singelton *getsingelton();//定义一个公有函数,可以获取这个唯一实例    static void print();    static int m_count;}; //将唯一指向实例的指针初始化为nullptrsingelton *singelton::single = nullptr;int singelton::m_count = 0;singelton *singelton::getsingelton(){    if(single == nullptr){//判断是不是第一次使用        single = new singelton;    }    return single;}void singelton::print(){    cout<<m_count<<endl;}int main(){singleton* a1 = singleton::getinstance();cout <ps婚纱照处理< a1 <&l喝啤酒为什么会胖t; endl;a1->print();singleton* a2 = singleton::getinstance();cout << a2 << endl;a2->print();system("pau");return 0;}

懒汉模式的singleton类有以下特点:

1.他有一个自然对数表指向唯一实例的静态指针,并且是私有的。

2.它有一个公有的函数,可以获取这个唯一的实例,并且在需要的时候创建该实例。

3.它的构造函数是私有的,这样就不能从别处创建该类的实例。

饿汉模式

饿汉模式在单例类定义的时候(即在main函数之前)就进行实例化。因为main函数执行之前,全局作用域的类成员静态变量m_instance已经初始化,故没有多线程的问题。

#include <iostream>#include <process.h>#include <windows.h>using namespace std;class singelton{private:    singelton(){        m_count ++;        printf("singe荸荠的最佳吃法和做法lton begin\n");        sleep(1000);                            // 加sleep为了放大效果        printf("singelton end\n");    }    static singelton *single;//定义一个唯一指向实例的指针,并且是私有的public:    static singelton *getsingelton();//定义一个公有函数,可以获取这个唯一实例    static void print();    static int m_count;};// 饿汉模式的关键:定义即实例化singelton *singelton::single = new singelton;int singelton::m_count = 0;singelton *singelton::getsingelton(){    // 不再需要进行实例化    //if(single == nullptr){    //    single = new singelton;    //}    return single;}void singelton::print(){    cout<<m_count<<endl;}int main(){cout << "we get the instance" << endl;singleton* a1 = singleton::getinstance();singleton* a2 = singleton::getinstance();singleton* a3 = singleton::getinstance();cout << "we destroy the instance" << endl;system("pau");return 0;}

线程安全的懒汉模式

在多线程环境下,懒汉模式的上述实现方式是不安全的,原因在于在判断instance是否为空时,可能存在多个线程同时进入if中,此时可能会实例化多个对象。于是出现了二重锁的懒汉模式,实现代码如下:

#include<iostream>#include<mutex>using namespace std;/*单例模式:构造函数私有化,对外提供一个接口*///线程安全的单例模式class lhsingleclass {public:static lhsingleclass* getinstance(){//双重锁模式if (instance == nullptr){//先判断是否为空,如果为空则进入,不为空说明已经存在实例,直接返回            //进入后加锁i_mutex.lock();if (instance == nullptr){//再判断一次,确保不会因为加锁期间多个线程同时进入instance = new lhsingleclass();}i_mutex.unlock();//解锁}return instance;}private:    static lhsingleclass* instance;static mutex i_mutex;//锁lhsingleclass(){}};lhsingleclass* lhsingleclass::instance=nullptr;mutex lhsingleclass::i_mutex;//类外初始化 int main(){lhsingle小学辅导班class* lhsinglep5 = lhsingleclass::getinstance();lhsingleclass* lhsinglep6 = lhsingleclass::getinstance();cout << lhsinglep5 << endl;cout << lhsinglep6 << endl;system("pau");return 0;}

此代码共进行了两次判断:

先判断是否为空,如果为空则进入,不为空说明已经存在实例,直接返回。再判断一次,确保不会因为加锁期间多个线程同时进入。

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!

本文发布于:2023-04-06 03:35:31,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/b06eac4ef31209726f59855298dbc1cd.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C++单例模式的懒汉模式和饿汉模式详解.doc

本文 PDF 下载地址:C++单例模式的懒汉模式和饿汉模式详解.pdf

标签:实例   懒汉   模式   函数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图