标签:懒汉 div ++ 单例模式 fine lock bsp class 懒汉模式
Foo.h
#ifndef FOO_H #define FOO_H class Foo { public: static Foo* getInstance(); ~Foo(); private: Foo(); static Foo* instance; }; #endif
Foo.cpp
#include "Foo.h"
#include <mutex>
std::mutex fooMutex; Foo* Foo::instance = NULL; Foo::Foo() { } Foo* Foo::getInstance() { fooMutex.lock(); if (NULL == instance) { instance = new Foo(); } fooMutex.unlock(); return instance; } Foo::~Foo() { if (NULL != instance) { delete instance; instance = NULL; } }
标签:懒汉 div ++ 单例模式 fine lock bsp class 懒汉模式
原文地址:https://www.cnblogs.com/adorkable/p/10512872.html