Design pattern (7) Singleton pattern

Introduction to singleton mode

Singleton mode, in the global, there can be only one class instance of simple interest mode, and a global access point is provided to access this unique instance.
It can be seen that the characteristics of the simple interest model are

  • There can only be one instance of this class
  • You must create this instance yourself
  • You must provide this instance to the entire system yourself

Singleton mode structure

The structure of the singleton pattern is very simple, containing only one class, the singleton class. To prevent the creation of multiple objects, its constructor must be private.
On the other hand, in order to provide a global access point to access the unique instance, the singleton class provides a public method getInstance to return the instance.
Insert picture description here

Single thread

The constructor of this class is private and contains a static member variable m_instance and a member function GetInstance(). Use GetInstance() to return the pointer of this class.

Last example:

#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;
}

It can be seen that the constructor is private, so the singleton mode can only be instantiated within the class. At the same time, the instance object instance is globally static, so it is guaranteed that the instance can only be created once.

Multithreading

Now consider the following multi-threaded situation, m_instance is empty, if two threads execute the function GetInstance() at the same time, then m_instance = new MyCAS() may be executed multiple times, and multiple objects will appear in the program, which is obviously contrary to The original intention of the singleton pattern.

#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()

The flag bit flag_once is set in call_once(). By recording this flag bit, it is determined whether the function has been called, thereby ensuring that the function is only called once. When another thread calls the function call_once() again, the function will no longer be executed.

#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;
}

Guess you like

Origin blog.csdn.net/qq_24649627/article/details/115213148