#ifndef _LP_SIGLETON_SYS_H_ #define _LP_SIGLETON_SYS_H_ #include #include namespace SystemLog { template class lp_singleton_sys { public: static T* instance() { T *sin = sys_this.load(std::memory_order_acquire); if (!sin) { std::lock_guard locker(sys_mutex); sin = sys_this.load(std::memory_order_relaxed); if (!sin) { sin = new T; sys_this.store(sin, std::memory_order_release); } } return sin; } static void uninstance() { T *sin = sys_this.load(std::memory_order_relaxed); if (sin) { std::lock_guard locker(sys_mutex); delete sin; sin = nullptr; } } protected: lp_singleton_sys() = default; virtual ~lp_singleton_sys() = default; private: lp_singleton_sys(const T&) = delete; T& operator=(const T&) = delete; static std::atomic sys_this; static std::mutex sys_mutex; }; template std::atomic lp_singleton_sys::sys_this; template std::mutex lp_singleton_sys::sys_mutex; } #endif