/
githubmirror
/
CPlusPlusThings
Обзор
Документация
Войти
/
githubmirror
/
CPlusPlusThings
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
design_pattern/singleton/dcl_singleton.cpp
43 строки
777 B
zhangxing
support bazel complie this project and format code.
30 мар 2024, 17:00
30 мар 2024, 17:00
3c8a3f2
Код
Авторство
О чём код?
// // Created by light on 20-2-7. // #include <iostream> using namespace std; #include <mutex> class singleton { private: singleton() {} static singleton *p; static mutex lock_; public: static singleton *instance(); // 实现一个内嵌垃圾回收类 class CGarbo { public: ~CGarbo() { if (singleton::p) delete singleton::p; } }; static CGarbo Garbo; // 定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数从而释放单例对象 }; singleton *singleton::p = nullptr; singleton::CGarbo Garbo; std::mutex singleton::lock_; singleton *singleton::instance() { if (p == nullptr) { lock_guard<mutex> guard(lock_); if (p == nullptr) p = new singleton(); } return p; }