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; }