From bdb9f6c86bd6c68ee76f133d8abd42d773186cfb Mon Sep 17 00:00:00 2001 From: wyc <1104749580@qq.com> Date: Thu, 23 Jul 2026 12:27:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- psc_global_include/Singleton.hpp | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) 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 +}