设计模式(7)单例模式

单例模式简介

单例模式,在全局中,仅可以有一个单利模式的类实例,并提供了一个全局访问点来访问这个唯一实例。
可以看出,单利模式的特点有

  • 这个类只能有一个实例
  • 必须自己创建这个实例
  • 必须自己向整个系统提供这个实例

单例模式结构

单例模式结构非常简单,只包含一个类,即单例类。为防止创建多个对象,其构造函数必须是私有的。
另一方面,为了提供一个全局访问点来访问该唯一实例,单例类提供了一个公有方法getInstance来返回该实例。
在这里插入图片描述

单线程

该类的构造函数为私有,并且包含一个静态的成员变量m_instance和成员函数GetInstance()。使用时通过GetInstance()返回该类的指针。

上个例子:

#include<iostream>
#include<thread>
#include<mutex>
#include<algorithm>

using namespace std;
 
class MyCAS
{
    
    
public:
	static MyCAS *GetInstance()
	{
    
    
		if (m_instance == NULL)
		{
    
    
			m_instance = new MyCAS();
      		cout << "MyCAS Set" << endl;
		}
    
		return m_instance;
	}	

private:
	MyCAS() {
    
    }

	static MyCAS *m_instance;
};
 
MyCAS *MyCAS::m_instance = NULL;

int main()
{
    
    
	MyCAS *ptr1 = MyCAS::GetInstance();
  	MyCAS *ptr2 = MyCAS::GetInstance();

	return 0;
}

可以看出,构造函数是私有的,这样单例模式就只能在类内部实现实例化。同时,实例对象instance是全局静态static的,因此保证了instance只能被创建一次。

多线程

现在考虑以下多线程的情况,m_instance为空,如果有两个线程同时执行函数GetInstance(),则m_instance = new MyCAS()可能会执行多次,程序中就会出现多个对象,这显然违背了单例模式的设计初衷。

#include <iostream>
#include <thread>
#include <mutex>
#include <algorithm>

using namespace std;

mutex resource_mutex;

class MyCAS
{
    
    
public:
  static MyCAS *GetInstance()
  {
    
    
    // double check
    if (m_instance == NULL)
    {
    
    
      unique_lock<mutex> guard(resource_mutex);
      if (m_instance == NULL)
      {
    
    
        m_instance = new MyCAS();
        cout << " MyCAS Set ! " << endl;
      }
    }
    return m_instance;
  }

private:
  MyCAS() {
    
    }
  static MyCAS *m_instance;
};

MyCAS *MyCAS::m_instance = NULL;

void MyFunc()
{
    
    
  cout << " Thread Start " << endl;
  MyCAS *ptr = MyCAS::GetInstance();
  cout << " Thread Stop " << endl;
}

int main()
{
    
    
  thread obj1(MyFunc);
  thread obj2(MyFunc);

  obj1.join();
  obj2.join();

  return 0;
}

call_once()

call_once()中设置有标志位flag_once,通过记录此标志位来确定函数是否被调用过,从而保证了函数只被调用了一次。其他线程再次调用函数call_once()时,就不会再执行该函数了。

#include <iostream>
#include <thread>
#include <mutex>
#include <algorithm>

using namespace std;

once_flag g_flag;

class MyCAS
{
    
    
public:
  static void CreatInstance()
  {
    
    
    m_instance = new MyCAS();
    cout << " MyCAS Set " << endl;
  }

  static MyCAS *GetInstance()
  {
    
    
    call_once(g_flag, CreatInstance);
    return m_instance;
  }

private:
  MyCAS() {
    
    }
  static MyCAS *m_instance;
};

MyCAS *MyCAS::m_instance = NULL;

void MyFunc()
{
    
    
  cout << " Thread Start " << endl;
  MyCAS *ptr = MyCAS::GetInstance();
  cout << " Thread Stop " << endl;
}

int main()
{
    
    
  thread obj1(MyFunc);
  thread obj2(MyFunc);

  obj1.join();
  obj2.join();
  
  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_24649627/article/details/115213148