"can some one confirm if this is a thread safe implementation of singleton" Code Answer

3

your implementation seems to be thread safe, but the simplest way to make a thread safe singleton looks like

class singleton {
    singleton();

public:
    ~singleton() {
    }

    static singleton* getinstance() {
        static singleton theinstance;
        return &theinstance;
    }
};

or better return a reference

    static singleton& getinstance() {
        static singleton theinstance;
        return theinstance;
    }

you don't need to reinvent the wheel here.

By H B on June 6 2022

Answers related to “can some one confirm if this is a thread safe implementation of singleton”

Only authorized users can answer the Search term. Please sign in first, or register a free account.