diff --git a/psc_global_include/Singleton.hpp b/psc_global_include/Singleton.hpp index f6c2802..38e2f11 100644 --- a/psc_global_include/Singleton.hpp +++ b/psc_global_include/Singleton.hpp @@ -1,37 +1,16 @@ #pragma once -#include -#include namespace Psc { template class Singleton { public: static Derived *instance() { - Derived *temp = instance_.load(std::memory_order_acquire); - if (temp == nullptr) { - std::lock_guard lock(mutex_); - temp = instance_.load(std::memory_order_relaxed); - if (temp == nullptr) { - temp = new Derived; - instance_.store(temp, std::memory_order_release); - } - } - return temp; - } - static void destroy() { - Derived* temp = instance_.exchange(nullptr, std::memory_order_acq_rel); - delete temp; // 若为 nullptr,delete 安全 + static Derived instance; + return &instance; } Singleton(const Singleton &) = delete; Singleton &operator=(const Singleton &) = delete; protected: Singleton() = default; ~Singleton() = default; - private: - static std::atomic instance_; - static std::mutex mutex_; }; - template - std::atomic Singleton::instance_(nullptr); - template - std::mutex Singleton::mutex_; -} \ No newline at end of file +}