From b3dd817d4f6a871b79ac95266b3ad9d26d4ca9f3 Mon Sep 17 00:00:00 2001 From: LSuper <120311070@qq.com> Date: Thu, 13 Jan 2022 10:59:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E5=8D=95=E4=BE=8B=E7=9A=84=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- design_pattern/singleton/README.md | 3 ++- design_pattern/singleton/dcl_singleton.cpp | 3 ++- design_pattern/singleton/static_local_singleton.cpp | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/design_pattern/singleton/README.md b/design_pattern/singleton/README.md index 4601823..b91b82e 100644 --- a/design_pattern/singleton/README.md +++ b/design_pattern/singleton/README.md @@ -114,7 +114,7 @@ private: static singleton *p; static mutex lock_; public: - singleton *instance(); + static singleton *instance(); // 实现一个内嵌垃圾回收类 class CGarbo @@ -131,6 +131,7 @@ public: singleton *singleton::p = nullptr; singleton::CGarbo Garbo; +std::mutex singleton::lock_; singleton* singleton::instance() { if (p == nullptr) { diff --git a/design_pattern/singleton/dcl_singleton.cpp b/design_pattern/singleton/dcl_singleton.cpp index 88278bb..2b22d03 100644 --- a/design_pattern/singleton/dcl_singleton.cpp +++ b/design_pattern/singleton/dcl_singleton.cpp @@ -14,7 +14,7 @@ private: static singleton *p; static mutex lock_; public: - singleton *instance(); + static singleton *instance(); // 实现一个内嵌垃圾回收类 class CGarbo @@ -31,6 +31,7 @@ public: singleton *singleton::p = nullptr; singleton::CGarbo Garbo; +std::mutex singleton::lock_; singleton* singleton::instance() { if (p == nullptr) { diff --git a/design_pattern/singleton/static_local_singleton.cpp b/design_pattern/singleton/static_local_singleton.cpp index ddab782..87626bc 100644 --- a/design_pattern/singleton/static_local_singleton.cpp +++ b/design_pattern/singleton/static_local_singleton.cpp @@ -11,12 +11,12 @@ private: static singleton *p; singleton() {} public: - singleton *instance(); + static singleton &instance(); }; -singleton *singleton::instance() { +singleton &singleton::instance() { static singleton p; - return &p; + return p; }