CPlusPlusThings/design_pattern/singleton/lock_singleton.cpp
Light-City 60af6600e4 update
2020-02-07 13:42:44 +08:00

26 lines
412 B
C++

//
// 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();
};
singleton *singleton::p = nullptr;
singleton* singleton::instance() {
lock_guard<mutex> guard(lock_);
if (p == nullptr)
p = new singleton();
return p;
}