-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.cpp
More file actions
47 lines (43 loc) · 1.31 KB
/
singleton.cpp
File metadata and controls
47 lines (43 loc) · 1.31 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
#include<iostream>
#include<mutex>
#include<thread>
using namespace std;
class Singleton {
public:
class GC {
public:
~GC() {
if (ptr != nullptr)
delete ptr;
}
};
static Singleton::GC g;
// 静态成员函数即使在类对象不存在的情况下也能被调用,静态函数只要使用类名加范围解析运算符::就可以访问。静态成员函数没有this指针,只能访问静态成员(包括静态成员变量和静态成员函数)。
static Singleton* getInstance(){
if(ptr==nullptr){
mtu.lock();
if(ptr==nullptr){
ptr = new Singleton();
}
mtu.unlock();
}
return ptr;
}
private:
static Singleton *ptr;
static mutex mtu;
Singleton() {};
Singleton(const Singleton&) = delete;
Singleton &operator=(const Singleton&) = delete;
};
Singleton* Singleton::ptr=nullptr;
mutex Singleton::mtu;
Singleton::GC g;
int main(){
thread t1([] { cout << Singleton::getInstance() << endl; });
thread t2([] { cout << Singleton::getInstance() << endl; });
t1.join();
t2.join();
cout << Singleton::getInstance() << endl;
return 0;
}