Untitled

 avatar
unknown
c_cpp
2 years ago
513 B
3
Indexable
class Singleton {
public:
    static Singleton& getInstance() {
        if (instance_ == nullptr) {
            std::lock_guard<std::mutex> lock(mutex_);
            if (instance_ == nullptr) {
                instance_ = new Singleton();
            }
        }
        return *instance_;
    }

    void doSomething() {
        // ...
    }

private:
    Singleton() {}

    static Singleton* instance_;
    static std::mutex mutex_;
};

Singleton* Singleton::instance_ = nullptr;
std::mutex Singleton::mutex_;