-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex7.cpp
91 lines (75 loc) · 2.38 KB
/
mutex7.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
using namespace std;
mutex myMutex;
class MySingleton {
public:
static MySingleton& getInstance() {
// This to make the singleton thread safe
lock_guard<mutex> myLock(myMutex);
if (!instance) instance = new MySingleton();
return *instance;
}
private:
MySingleton();
~MySingleton();
//No copy operator
MySingleton(const MySingleton&) = delete;
MySingleton& operator= (const MySingleton&) = delete;
static MySingleton* instance;
};
int main() {
/*
Problem: Each time I use my singleton in one of the below ways, it will lock the mutex which is slow, especially if I just read from it.
MySingleton::MySingleton()= default;
MySingleton::~MySingleton()= default;
MySingleton* MySingleton::instance= nullptr;
...
MySingleton::getInstance();
*/
//
// Additionally the
// new MySingleton();
// ------------------- is NOT atomic;
// Operations :
// 1) Allocate memory for MySingleton
// 2) Create MySingleton object in memory
// 3) Refer instance to MySingleton object
// Possible execution order 1, 3, 2
return 0;
}
// Solutions
// 1) call_once and once_flags
// class MySingleton1 {
// public:
// static MySingleton1& getInstance() {
// Will be invoked only once
// call_once(initInstanceFlag, &MySingleton1::initSingleton);
// return *instance;
// }
// private:
// MySingleton1();
// ~MySingleton1();
// MySingleton1(const MySingleton1&) = delete;
// MySingleton1& operator= (const MySingleton1&) = delete;
// Important
// static once_flag initInstanceFlag;
// static void initSingleton() {
// instance = new MySingleton1;
// }
// static MySingleton1* instance;
// };
//...
// once_flag MySingleton1::initInstanceFlag;
// 2) Meyers MySingleton
// class MySingleton {
// public:
// static MySingleton& getInstance() {
// static variable are thread safe by nature
// static MySingleton instance;
// return *instance;
// }
// ...
// 3) Atomic Variables