有人可以确认这是否是单例的线程安全实现吗

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

#include "iostream"
#include "atomic"

using namespace std;

class Singleton
{
    Singleton();


    static Singleton * _pInstance;

    public:
       ~Singleton() {
       }

       static Singleton* getInstance() {

          Singleton * tmp = _pInstance;

          atomic_thread_fence(std::memory_order_acquire);

          if (tmp == nullptr){

             tmp = _pInstance;

             if (!tmp) {

                _pInstance = new Singleton();

                atomic_thread_fence(std::memory_order_release);

                _pInstance = tmp;
             }

         return _pInstance;
     }
};

Singleton* Singleton::_pInstance = nullptr;

您的实现似乎是线程安全的,但制作线程安全单例的最简单方法看起来像

class Singleton {
    Singleton();

public:
    ~Singleton() {
    }

    static Singleton* getInstance() {
        static Singleton theInstance;
        return &theInstance;
    }
};

或更好return参考

    static Singleton& getInstance() {
        static Singleton theInstance;
        return theInstance;
    }

你不需要在这里重新发明轮子。