lundi 22 février 2021

singleton and new operator - thread safety

I know that Scott Meyers Singleton is thread safe for C++11 and above. Does the following code is thread safe (for C++11 and above) ?

class T
{
    static T* obj;
public:
    T& GetInstance()
    {
        if (!obj)
        {
            obj = new T();
        }
        return *obj;
    }
};
T* T::obj=nullptr;

Aucun commentaire:

Enregistrer un commentaire