Details and examples of the C++ singleton pattern

  • 2020-05-26 09:51:04
  • OfStack

Details and examples of the C++ singleton pattern

1. What is a singleton pattern?

The singleton pattern, also known as the singleton pattern and the singleton pattern, is probably the most widely used design pattern. The intent is to ensure that there is only one instance of a class and to provide one global access point to it, which is Shared by all program modules. There are many places that need such functional modules, such as system log output, GUI application must be a single mouse, MODEM connection requires 1 and only 1 phone line, the operating system can only have 1 window manager, 1 PC with 1 keyboard.
Through the singleton pattern, you can:

(1) ensure that only one instance of a class is created
(2) provides a global access pointer to the object
(3) allow multiple instances in the future without affecting the client side of the singleton class

2. How do you implement the singleton pattern

How to implement a simple singleton pattern: the constructor is declared as private or protect to prevent it from being instantiated by an external function, and an private static class pointer is stored internally to hold a singleton instance. The action of the instance is performed by an public class method, which also returns a singleton instance.

3. Concrete implementation

(1) the simplest singleton model (lazy model)

Lazy mode: lazy is characterized by lazy loading, such as configuration files, using the lazy method, as the name implies, lazy, very lazy, configuration file instances will not be loaded until used.


class SiglenTon
{
public:
  static SiglenTon *GetInstence()
  {
    if( p == NULL)
    {
      p = new SiglenTon();
    }
    return p;
  }
private:
  SiglenTon()
  {
    cout<<"SiglenTon()"<<endl;
  }
  static SiglenTon *p;
};
SiglenTon *SiglenTon::p = NULL;
int main()
{
  SiglenTon *p = SiglenTon::GetInstence();
  return 0;
}

(2) thread safety is not considered in the first method


class SiglenTon //( Thread-safe hungry Chinese mode )
{
public:
  static SiglenTon *GetInstence()
  {
    cout<<"static SiglenTon *GetInstence()"<<endl;
    pthread_mutex_lock(&mutex);
    if( p == NULL)
    {
      pthread_mutex_lock(&mutex);
      p = new SiglenTon();
      pthread_mutex_unlock(&mutex);
    }
    pthread_mutex_unlock(&mutex);
    return p;
  }

private:
  SiglenTon()
  {
    cout<<"SiglenTon()"<<endl;
  }
  static SiglenTon *p;
};
SiglenTon *SiglenTon::p = NULL;



int main()
{
  SiglenTon *p = SiglenTon::GetInstence();
  SiglenTon *p1 = p->GetInstence();

  return 0;
}

(3) hunhan mode (hunhan mode itself is thread safe)


class SiglenTon
{
public:
  SiglenTon *GetInstence()
  {
    if( p == NULL)
    {
      return p;
    }
  }
private:
  SiglenTon()
  {
    cout<<"SiglenTon()"<<endl;
  }
  static SiglenTon *p;
};

The above is the explanation of the singleton mode in C++, if you have any questions, please leave a message or go to the community of this site to exchange and discuss, thank you for reading, hope to help you, thank you for your support to this site!


Related articles: