What are the methods for implementing the singleton pattern in C++?

In C++, there are several common ways to implement the singleton pattern.

  1. Eager singleton pattern: creates a singleton object at the program’s start and provides a public access method. The downside is that creating the object at program start may slow down the program’s startup speed.
class Singleton {
private:
    static Singleton* instance;
    Singleton() {}

public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }
};

Singleton* Singleton::instance = nullptr;
  1. Lazy initialization singleton pattern: the singleton object is only created when it is first accessed. The downside of this method is the need for additional thread synchronization mechanisms to ensure thread safety in a multi-threaded environment.
class Singleton {
private:
    static Singleton* instance;
    Singleton() {}

public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }
};

Singleton* Singleton::instance = nullptr;
  1. Double-checked locking singleton pattern: This pattern enhances the lazy initialization singleton pattern by adding a double-check mechanism, ensuring thread safety in a multi-threaded environment and reducing the frequency of lock usage.
class Singleton {
private:
    static Singleton* instance;
    static std::mutex mtx;
    Singleton() {}

public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            std::lock_guard<std::mutex> lock(mtx);
            if (instance == nullptr) {
                instance = new Singleton();
            }
        }
        return instance;
    }
};

Singleton* Singleton::instance = nullptr;
std::mutex Singleton::mtx;
  1. Static local variable singleton pattern: Using the static local variable feature in C++, the singleton object is created when needed and ensures thread safety.
class Singleton {
private:
    Singleton() {}

public:
    static Singleton* getInstance() {
        static Singleton instance;
        return &instance;
    }
};
bannerAds