前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c 线程安全的单例模式-详解C++实现线程安全的单例模式

c 线程安全的单例模式-详解C++实现线程安全的单例模式

作者头像
囍楽云
发布2022-12-29 13:59:34
8060
发布2022-12-29 13:59:34
举报
文章被收录于专栏:囍楽云博客囍楽云博客

  在某些应用环境下面,一个类只允许有一个实例,这就是著名的单例模式。单例模式分为懒汉模式,跟饿汉模式两种。

  首先给出饿汉模式的实现

  正解:

代码语言:javascript
复制
template 
class singleton
{
protected:
  singleton(){};
private:
  singleton(const singleton&){};//禁止拷贝
  singleton& operator=(const singleton&){};//禁止赋值
  static T* m_instance;
public:
  static T* GetInstance();
};template 
T* singleton::GetInstance()
{
  return m_instance;
}template 

  在实例化 变量时,直接调用类的构造函数。顾名思义,在还未使用变量时,已经对进行赋值,就像很饥饿的感觉。这种模式,在多线程环境下肯定是线程安全的,因为不存在多线程实例化的问题。

  下面来看懒汉模式

代码语言:javascript
复制

template 
class singleton
{
protected:
  singleton(){};
private:
  singleton(const singleton&){};
  singleton& operator=(const singleton&){};static T* m_instance;
public:
  static T* GetInstance();
};
template 
T* singleton::GetInstance()
{
  if( m_instance == NULL)
  {
m_instance = new T();
}
  return m_instance;
}
template 
T* singleton::m_instance = NULL;

  懒汉模式下,在定义变量时先等于NULL,在调用()方法时c 线程安全的单例模式,在判断是否要赋值。这种模式,并非是线程安全的,因为多个线程同时调用()方法,就可能导致有产生多个实例。要实现线程安全,就必须加锁。

  下面给出改进之后的代码

代码语言:javascript
复制
template     class singleton    {    protected:        singleton(){};    private:        singleton(const singleton&){};        singleton& operator=(const singleton&){};        static T m_instance;        static pthread_mutex_t mutex;    public:        static T GetInstance();    };              template     T singleton::GetInstance()    {        pthread_mutex_lock(&mutex);        if( m_instance == NULL)        {             m_instance = new T();        }        pthread_mutex_unlock(&mutex);        return m_instance;    }              template     pthread_mutex_t singleton::mutex = PTHREAD_MUTEX_INITIALIZER;         template     T singleton::m_instance = NULL;    

  这一切看起来都很完美,但是程序猿是一种天生就不知道满足的动物。他们发现()方法,每次进来都要加锁,会影响效率。然而这并不是必须的c 线程安全的单例模式,于是又对()方法进行改进

代码语言:javascript
复制

template 
T* singleton::GetInstance()
{
  if( m_instance == NULL)
  {pthread_mutex_lock(&mutex);
if( m_instance == NULL)
{ 
   m_instance = new T();
}

pthread_mutex_unlock(&mutex);
}
  return m_instance;
}

  这也就是所谓的“双检锁”机制。但是有人质疑这种实现还是有问题,在执行 = new T()时,可能 类T还没有初始化完成, 就已经有值了。这样会导致另外一个调用()方法的线程,获取到还未初始化完成的 指针,如果去使用它,会有意料不到的后果。其实,解决方法也很简单,用一个局部变量过渡下即可:

  正解:

代码语言:javascript
复制

template 
T* singleton::GetInstance()
{
  if( m_instance == NULL)
  {pthread_mutex_lock(&mutex);
if( m_instance == NULL)
{ 
   T* ptmp = new T();
   m_instance = ptmp;
}
pthread_mutex_unlock(&mutex);
}
  return m_instance;
}

  到这里在懒汉模式下,也就可以保证线程安全了。

  然而,在linux下面还有另一种实现。linux提供了一个叫()的函数,它保证在一个进程中,某个函数只被执行一次。下面是使用实现的线程安全的懒汉单例模式

代码语言:javascript
复制
template 
class singleton
{
protected:
  singleton(){};
private:
  singleton(const singleton&){};
  singleton& operator=(const singleton&){};
  static T* m_instance;static pthread_once_t m_once;
public:
  static void Init();
  static T* GetInstance();
};template 
void singleton::Init()
{
  m_instance = new T();
}template 
T* singleton::GetInstance()
{
  pthread_once(&m_once,Init);
  return m_instance;
}template 
pthread_once_t singleton::m_once = PTHREAD_ONCE_INIT;template 
T* singleton::m_instance = NULL;

  上面的单例类使用了模板,对每一种类型的变量都能实例化出唯一的一个实例。

  例如要实例化一个int类型

代码语言:javascript
复制
int *p = singleton::GetInstance()

  例如要实例化一个string类型

代码语言:javascript
复制
string *p = singleton::GetInstance()

  在上面的实现中,在实例化对象时,调用()函数时都没有传递参数,这是犹豫不同的对象其初始化时参数个数都不一样。如果要支持不同类型的对象带参数初始化,则需要重载函数。然而在c++11中,已经支持了可变参数函数。这里给出一个简单的例子

代码语言:javascript
复制
ifndef SINGLETON_H
define SINGLETON_H
template 
class singleton
{
protected:
  singleton(){};
private:
  singleton(const singleton&){};
  singleton& operator=(const singleton&){};
  static T* m_instance;
public:
  template 
  static T* GetInstance(Args&&... args)
  {
if(m_instance == NULL)
  m_instance = new T(std::forward(args)...);
return m_instance;
}
static void DestroyInstance()
  {
if(m_instance )
  delete m_instance;
m_instance = NULL;
}
};
template 
T* singleton::m_instance = NULL;
endif

  测试函数

代码语言:javascript
复制
#include 
#include 
#include "singleton.h"
 
using namespace std;
struct A
{
  A(int a ,int b):_a(a),_b(b)
  {}
  int _a;
  int _b;
};
 
int main()
{
  int *p1 = singleton::GetInstance(5);
  int *p2 = singleton::GetInstance(10);
  cout 
[1]: https://xuan.ddwoo.top/index.php/archives/564/
[2]: https://xuan.ddwoo.top/index.php/archives/565/                
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档