mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-03 10:33:25 +08:00
update
This commit is contained in:
25
design_pattern/singleton/lock_singleton.cpp
Normal file
25
design_pattern/singleton/lock_singleton.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
Reference in New Issue
Block a user