Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
513 B
2
Indexable
Never
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_;