设计模式笔记:单例模式

单例模式介绍

单例模式:只能获取该类唯一一个实例对象
1.构造函数私有化
2.定义该类唯一的对象
3.通过static静态成员方法返回该类唯一的实例对象

常用到比如日志模块、数据库模块

饿汉单例模式:程序启动时实例化对象

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
#include <iostream>
using namespace std;

class Singleton
{
public:
static Singleton* GetInstance()
{
return &instance;
}
private:
static Singleton instance; // 定义唯一的类的实例对象
Singleton() {} // 构造函数私有化
~Singleton() {}
Singleton(const Singleton&) = delete; // 防止从唯一实例拷贝构造新对象
Singleton& operator= (const Singleton&) = delete;
};

Singleton Singleton::instance;

int main()
{
Singleton *singOne = Singleton::GetInstance();
Singleton *singTwo = Singleton::GetInstance();
Singleton *singThree = Singleton::GetInstance();

cout << singOne << " " << singTwo << " " << singThree << endl;
return 0;
}


三个指针的地址是一样的,说明访问同一个对象

懒汉单例模式:推迟到第一次访问时实例化对象

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
#include <iostream>
using namespace std;

class Singleton
{
public:
static Singleton* GetInstance()
{
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
private:
static Singleton* instance;
Singleton() {}
~Singleton() {}
Singleton(const Singleton&) = delete;
Singleton& operator= (const Singleton&) = delete;
};

Singleton* Singleton::instance = nullptr;

int main()
{
Singleton *singOne = Singleton::GetInstance();
Singleton *singTwo = Singleton::GetInstance();
Singleton *singThree = Singleton::GetInstance();

cout << singOne << " " << singTwo << " " << singThree << endl;
return 0;
}

单例模式的线程安全问题

1. 饿汉单例模式本身是线程安全的

static静态对象,是在程序启动时,main函数运行前初始化好的,因此不存在线程安全问题

2. 懒汉单例模式不是线程安全的

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
#include <iostream>
#include <thread>
using namespace std;

class Singleton
{
public:
static Singleton* GetInstance()
{
if (instance == nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
instance = new Singleton();
}
return instance;
}
private:
static Singleton* instance;
Singleton() {}
~Singleton() {}
Singleton(const Singleton&) = delete;
Singleton& operator= (const Singleton&) = delete;
};

Singleton* Singleton::instance = nullptr;

int main()
{
Singleton* singleOne = nullptr;
Singleton* singleTwo = nullptr;
thread t1([&singleOne]() {singleOne = Singleton::GetInstance(); });
thread t2([&singleTwo]() {singleTwo = Singleton::GetInstance(); });
t1.join();
t2.join();
cout << singleOne << " " << singleTwo << endl;
return 0;
}

结果是两个实例,原因在于instance= new CSingleton()会做三件事情,开辟内存,调用构造函数,给instance指针赋值
多线程环境下可能出现t1、t2线程都通过了if (instance == nullptr) 判断,那么就new了两个对象,就不符合单例模式唯一实例要求了(我通过加100毫秒延时模拟了这种情况)

3. 懒汉单例模式通过 锁+双重判断 实现线程安全

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
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

std::mutex mtx;

class Singleton
{
public:
static Singleton* GetInstance()
{
if (instance == nullptr) {
mtx.lock(); // 获取互斥锁
if (instance == nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
instance = new Singleton();
}
mtx.unlock(); // 释放互斥锁
}
return instance;
}
private:
static Singleton* instance;
Singleton() {}
~Singleton() {}
Singleton(const Singleton&) = delete;
Singleton& operator= (const Singleton&) = delete;
};

Singleton* Singleton::instance = nullptr;

int main()
{
Singleton* singleOne = nullptr;
Singleton* singleTwo = nullptr;
thread t1([&singleOne]() {singleOne = Singleton::GetInstance(); });
thread t2([&singleTwo]() {singleTwo = Singleton::GetInstance(); });
t1.join();
t2.join();
cout << singleOne << " " << singleTwo << endl;
return 0;
}


当前多线程环境下访问了同一个实例对象,是线程安全的
核心修改:锁 + 双重判断

1
2
3
4
5
6
7
8
9
10
11
12
static Singleton* GetInstance()
{
if (instance == nullptr) {
mtx.lock(); // 获取互斥锁
if (instance == nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
instance = new Singleton();
}
mtx.unlock(); // 释放互斥锁
}
return instance;
}

如果使用锁 + 单重判断也是可以的,就是效率会低一些(每次进入GetInstance都会先加锁)

1
2
3
4
5
6
7
8
9
10
static Singleton* GetInstance()
{
mtx.lock(); // 获取互斥锁
if (instance == nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
instance = new Singleton();
}
mtx.unlock(); // 释放互斥锁
return instance;
}

单例对象的释放

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
#include <iostream>
using namespace std;

class Singleton
{
public:
static Singleton* GetInstance()
{
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
private:
static Singleton* instance;
Singleton() { cout << "Singleton()" << endl; }
~Singleton() { cout << "~Singleton()" << endl; }
Singleton(const Singleton&) = delete;
Singleton& operator= (const Singleton&) = delete;
// 定义嵌套类,在该类的析构函数中,自动释放外层类的资源
class Release
{
public:
~Release() {
delete instance;
}
};
// 通过静态对象在程序结束时自动析构特点,来释放外层类的instance资源
static Release release;
};

Singleton* Singleton::instance = nullptr;
Singleton::Release Singleton::release;

int main()
{
Singleton* singOne = Singleton::GetInstance();
Singleton* singTwo = Singleton::GetInstance();
Singleton* singThree = Singleton::GetInstance();

cout << singOne << " " << singTwo << " " << singThree << endl;
return 0;
}


成功调用了Singleton的析构函数

总结

单例模式有饿汉单例和懒汉单例两种
饿汉单例的实例对象在调用前就构造好了
懒汉单例的实例对象是推迟到第一次访问时构造
饿汉单例模式是线程安全的、懒汉单例模式要通过锁 + 双重判断保证线程安全(如果使用锁 + 单重判断,那么每次进入GetInstance函数都会先加锁,会效率低)