设计模式-单例模式(懒汉+饿汉+线程安全模式)

目录

单例模式

单例模式可以说是23种设计模式中最为简单的一种设计模式

  1. 类中只有唯一一个实例并且是私有化的,只能通过公有的静态函数获取,并且构造函数私有化,防止被外部实例化
  2. 提供静态的公有函数用来获取访问唯一的一个实例

如果说类中的某一个资源是属于类本身的,而不是所属类的某一个对象的,我们就可以把这个资源设置为static, 类比来说,如果某一个类的成员都是所属于类本身,并且要公共使用的话,我们就可以把这个类设置为单例模式,并且提供唯一的static实例 公共使用

使用场景:常用于数据库类,日志类的实现

最简单的单例模式类:

复制代码
class single{
 private:
     //私有静态指针变量指向唯一实例
     static single *p;
 
     //私有化构造函数
    single(){}
    ~single(){}

public:
    //公有静态方法获取实例
    static single* getinstance();
};

懒汉模式

单例模式类中有唯一一个实例的指针,懒汉模式,即第一次使用时才初始化这个单例模式的指针

复制代码
class single{
 private:
     //私有静态指针变量指向唯一实例
     static single *p;
 
     //私有化构造函数
    single(){}
    ~single(){}

public:
    //公有静态方法获取实例
    static single* getinstance();
};

single* single::p = NULL;
single* single::getinstance(){
        if (NULL == p){
            p = new single;  
        }
    return p;
}

在调用getinstance函数获取唯一实例的时候才进行第一次初始化.

饿汉模式

饿汉模式顾名思义非常饿,它是在程序一开始的时候,就对唯一的实例初始化了

复制代码
class single{
 private:
     //私有静态指针变量指向唯一实例
     static single *p;
 
     //私有化构造函数
    single(){}
    ~single(){}

public:
    //公有静态方法获取实例
    static single* getinstance();
};
single* single::p = new single();
single* single::getinstance(){
    return p;
}

线程安全的懒汉模式

之前的懒汉模式并没有提到多线程安全的情况,如果现在有多个线程使用单例模式类,就有可能会导致多个线程对该实例的多次初始化,这就产生错误了,接下来给出线程安全版本

双重检查锁定(过时,错误的版本)

复制代码
class single{
 private:
     //私有静态指针变量指向唯一实例
    static single *p;
    
    static pthread_mutex_t lock;//定义一个互斥锁
     //私有化构造函数
    single(){
          pthread_mutex_init(&lock, NULL);
    }
    ~single(){}

public:
    //公有静态方法获取实例
    static single* getinstance();
};

pthread_mutex_t single::lock;

single* single::p = NULL;
single* single::getinstance(){
    if (NULL == p){
        pthread_mutex_lock(&lock);
        if (NULL == p){
            p = new single;
        }
        pthread_mutex_unlock(&lock);
    }
    return p;
}

这种保证线程安全的方式叫做双重检查锁定,使用互斥锁对new进行保护,又为了防止每次调用函数时都需要加锁,在锁的外围又加了一层 if判断,但是这种做法后来被证实是错误的,并不能完全保证线程安全

详细参考Scott Meyers 与 Andrei Alexandrescu 的"C++ and the Perils of Double-Checked Locking"这篇文章

C++11 特有的线程安全懒汉模式

复制代码
 class single{
 private:
     single(){}
     ~single(){}
 
 public:
     static single* getinstance();
 };

single* single::getinstance(){
   static single obj;
   return &obj;
}

这种方式不需要加互斥锁,也能保护线程安全,算是最好的一种实现方式.

复制代码
 class single{
 private:
     single(){}
     ~single(){}
     
 void my_print_private()
 { 
    cout<<"helloworld"<<endl;
 }
 
 public:
     static single* getinstance();
     
     static void my_print_public()  //这里也可以改成不是static,函数体直接调用my_print_private即可
     {
          single::getinstance()->my_print_private();
     }
 };

single* single::getinstance(){
   static single obj;
   return &obj;
}
相关推荐
Murphy_lx2 分钟前
单例模式_
单例模式
phdsky14 分钟前
【设计模式】建造者模式
c++·设计模式·建造者模式
小毛驴85020 分钟前
软件设计模式-装饰器模式
python·设计模式·装饰器模式
phdsky2 小时前
【设计模式】代理模式
设计模式·代理模式
ZHE|张恒13 小时前
设计模式(十二)代理模式 — 用代理控制访问,实现延迟加载、权限控制等功能
设计模式·代理模式
SakuraOnTheWay14 小时前
《深入设计模式》学习(1)—— 深入理解OOP中的6种对象关系
设计模式
q***718514 小时前
Java进阶-SpringCloud设计模式-工厂模式的设计与详解
java·spring cloud·设计模式
白衣鸽子14 小时前
告别参数地狱:业务代码中自定义Context的最佳实践
后端·设计模式·代码规范
wheeldown14 小时前
【Linux】多线程核心速记:线程池 + 单例模式 + 线程安全 + 死锁 + 智能指针
linux·运维·服务器·安全·单例模式
帅中的小灰灰1 天前
C++编程建造器设计模式
java·c++·设计模式