修复设计模式-单例的一些错误

This commit is contained in:
LSuper 2022-01-13 10:59:16 +08:00
parent 388564c9d9
commit b3dd817d4f
3 changed files with 7 additions and 5 deletions

View File

@ -114,7 +114,7 @@ private:
static singleton *p; static singleton *p;
static mutex lock_; static mutex lock_;
public: public:
singleton *instance(); static singleton *instance();
// 实现一个内嵌垃圾回收类 // 实现一个内嵌垃圾回收类
class CGarbo class CGarbo
@ -131,6 +131,7 @@ public:
singleton *singleton::p = nullptr; singleton *singleton::p = nullptr;
singleton::CGarbo Garbo; singleton::CGarbo Garbo;
std::mutex singleton::lock_;
singleton* singleton::instance() { singleton* singleton::instance() {
if (p == nullptr) { if (p == nullptr) {

View File

@ -14,7 +14,7 @@ private:
static singleton *p; static singleton *p;
static mutex lock_; static mutex lock_;
public: public:
singleton *instance(); static singleton *instance();
// 实现一个内嵌垃圾回收类 // 实现一个内嵌垃圾回收类
class CGarbo class CGarbo
@ -31,6 +31,7 @@ public:
singleton *singleton::p = nullptr; singleton *singleton::p = nullptr;
singleton::CGarbo Garbo; singleton::CGarbo Garbo;
std::mutex singleton::lock_;
singleton* singleton::instance() { singleton* singleton::instance() {
if (p == nullptr) { if (p == nullptr) {

View File

@ -11,12 +11,12 @@ private:
static singleton *p; static singleton *p;
singleton() {} singleton() {}
public: public:
singleton *instance(); static singleton &instance();
}; };
singleton *singleton::instance() { singleton &singleton::instance() {
static singleton p; static singleton p;
return &p; return p;
} }