-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.cpp
More file actions
76 lines (71 loc) · 1.76 KB
/
Singleton.cpp
File metadata and controls
76 lines (71 loc) · 1.76 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <atomic>
#include <mutex>
using namespace std;
//懒汉 double check lock,会有re-order的问题,考虑C++11的memory model
class Singleton1 {
private:
static atomic<Singleton1*> singleton;
static mutex m_mutex;
private:
Singleton1() {}
Singleton1(const Singleton1& other) {}
Singleton1& operator=(const Singleton1& other) {}
~Singleton1() {}
public:
static Singleton1* getInstance() {
if (singleton == nullptr) {
lock_guard<mutex> lock(m_mutex); // 自解锁
if (singleton == nullptr) {
singleton = new Singleton1();
}
}
return singleton;
}
};
atomic<Singleton1*> Singleton1::singleton = nullptr;
mutex Singleton1::m_mutex;
//饿汉
class Singleton2 {
private:
static Singleton2* singleton;
private:
Singleton2() {}
Singleton2(const Singleton2& other) {}
Singleton2& operator=(const Singleton2& other) {}
~Singleton2() {}
public:
static Singleton2* getInstance() {
return singleton;
}
};
Singleton2* Singleton2::singleton = new Singleton2();
//Meyers' Singleton
class Singleton3 {
private:
Singleton3() {}
Singleton3(const Singleton3& other) {}
Singleton3& operator=(const Singleton3& other) {}
~Singleton3() {}
public:
static Singleton3* getInstance() {
static Singleton3 singleton;
return &singleton;
}
};
int main()
{
Singleton1 *s1 = Singleton1::getInstance();
Singleton1 *s2 = Singleton1::getInstance();
if (s1 == s2)
cout << "success" << endl;
Singleton2 *s3 = Singleton2::getInstance();
Singleton2 *s4 = Singleton2::getInstance();
if (s3 == s4)
cout << "success" << endl;
Singleton3 *s5 = Singleton3::getInstance();
Singleton3 *s6 = Singleton3::getInstance();
if (s5 == s6)
cout << "success" << endl;
return 0;
}