-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.cpp
More file actions
42 lines (38 loc) · 1.17 KB
/
singleton.cpp
File metadata and controls
42 lines (38 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <memory>
#include <future>
using namespace std;
class Singleton {
public:
// 析构函数设置为私有, 这里需要声明友元
friend class default_delete<Singleton>;
// 获取实例接口
static Singleton& get_instance() {
// 懒汉模式一次性初始化, static 注意下
static once_flag flag;
call_once(flag, [&]() {
// make_unique 需要声明友元
cout << "only called once" << endl;
_instance = unique_ptr<Singleton>(new Singleton());
});
return *_instance;
}
private:
// 防止产生多个实例对象
Singleton() = default;
// 防止意外析构
~Singleton() = default;
Singleton(const Singleton&) = default;
Singleton& operator=(const Singleton&) = default;
private:
// 声明为私有防止直接访问
// 声明为只能指针, 防止内存泄露
static unique_ptr<Singleton> _instance;
};
unique_ptr<Singleton> Singleton::_instance = nullptr;
int main() {
Singleton& ins1 = Singleton::get_instance();
future<Singleton &> fut = async(Singleton::get_instance);
Singleton& ins2 = fut.get();
return 0;
}