Skip to content

Commit 60af660

Browse files
committed
update
1 parent 5b5e3bc commit 60af660

17 files changed

+505
-10
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,13 @@
137137
- [myhashtable](./stl_src/myhashtable.md)
138138
- [unordered_map](./stl_src/unordered_map.md)
139139
140-
## 3.多线程与多进程
140+
## 3.设计模式
141141
142-
### 3.1 Threading In C++
142+
- [单例模式](./design_pattern/singleton)
143+
144+
## 4.多线程与多进程
145+
146+
### 4.1 Threading In C++
143147
144148
- [介绍](./Threading_In_CPlusPlus/1.thread)
145149
- [创建线程的五种类型](./Threading_In_CPlusPlus/2.create_type)
@@ -150,9 +154,9 @@
150154
>
151155
> https://www.youtube.com/watch?v=eZ8yKZo-PGw&list=PLk6CEY9XxSIAeK-EAh3hB4fgNvYkYmghp&index=4
152156
153-
### 4.学习课程
157+
### 5.学习课程
154158
155-
#### 4.1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
159+
#### 5.1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
156160
157161
- [堆、栈、RAII:C++里该如何管理资源?](./modern_C++_30/RAII)
158162
- [堆](./modern_++_30/RAII/heap.cpp)
@@ -178,10 +182,11 @@
178182
- [SFINAE:不是错误的替换失败是怎么回事?](./modern_C++_30/SFINAE)
179183
- [constexpr:一个常态的世界](./modern_C++_30/constexpr)
180184
- [函数对象和lambda:进入函数式编程](./modern_C++_30/functionLambda)
185+
- [内存模型和atomic:理解并发的复杂性](./modern_C++_30/memorymodel_atomic)
181186
182-
### 5.拓展部分
187+
### 6.拓展部分
183188
184-
#### 5.1 [C++惯用法](./codingStyleIdioms)
189+
#### 6.1 [C++惯用法](./codingStyleIdioms)
185190
186191
##### 你最喜欢的c++编程风格惯用法是什么?
187192
@@ -191,11 +196,11 @@
191196
- [4.copy and swap](./codingStyleIdioms/4_copy-swap)
192197
- [5.pImpl(指针指向具体实现)](./codingStyleIdioms/5_pImpl)
193198
194-
#### 5.2 一些问题
199+
#### 6.2 一些问题
195200
196201
- [C++中如何将string类型转换为int类型?](./basic_content/extent/string_int.md)
197202
198-
### 6.工具篇
203+
### 7.工具篇
199204
200205
- [容器快捷输出工具](./tool/output)
201206
@@ -225,7 +230,7 @@
225230
226231
- [https://cppinsights.io](https://cppinsights.io/)
227232
228-
### 7.代码运行
233+
### 8.代码运行
229234
230235
- **代码环境**
231236
@@ -235,7 +240,7 @@
235240
236241
CLion gcc/g++
237242
238-
### 8.关于作者
243+
### 9.关于作者
239244
240245
个人公众号:
241246
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Created by light on 20-2-7.
3+
//
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
#include <mutex>
9+
10+
#define barrier() __asm__ volatile ("lwsync")
11+
12+
// method 1 operator new + placement new
13+
//singleton *instance() {
14+
// if (p == nullptr) {
15+
// lock_guard<mutex> guard(lock_);
16+
// if (p == nullptr) {
17+
// singleton *tmp = static_cast<singleton *>(operator new(sizeof(singleton)));
18+
// new(p)singleton();
19+
// p = tmp;
20+
// }
21+
// }
22+
// return p;
23+
//}
24+
class singleton {
25+
private:
26+
singleton() {}
27+
28+
static singleton *p;
29+
static mutex lock_;
30+
public:
31+
static singleton *instance();
32+
};
33+
34+
singleton *singleton::p = nullptr;
35+
36+
singleton *singleton::instance() {
37+
if (p == nullptr) {
38+
lock_guard<mutex> guard(lock_);
39+
barrier();
40+
if (p == nullptr) {
41+
p = new singleton();
42+
}
43+
}
44+
return p;
45+
}
46+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Created by light on 20-2-7.
3+
//
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
#include <mutex>
10+
#include <atomic>
11+
12+
//C++ 11版本之后的跨平台实现
13+
class singleton {
14+
private:
15+
singleton() {}
16+
17+
static mutex lock_;
18+
static atomic<singleton *> p;
19+
public:
20+
singleton *instance();
21+
};
22+
23+
mutex singleton::lock_;
24+
atomic<singleton *> singleton::p;
25+
26+
/*
27+
* std::atomic_thread_fence(std::memory_order_acquire);
28+
* std::atomic_thread_fence(std::memory_order_release);
29+
* 这两句话可以保证他们之间的语句不会发生乱序执行。
30+
*/
31+
singleton *singleton::instance() {
32+
singleton *tmp = p.load(memory_order_relaxed);
33+
atomic_thread_fence(memory_order_acquire);
34+
if (tmp == nullptr) {
35+
lock_guard<mutex> guard(lock_);
36+
tmp = p.load(memory_order_relaxed);
37+
if (tmp == nullptr) {
38+
tmp = new singleton();
39+
atomic_thread_fence(memory_order_release);
40+
p.store(tmp, memory_order_relaxed);
41+
}
42+
}
43+
return p;
44+
}
45+
46+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Created by light on 20-2-7.
3+
//
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
#include <mutex>
9+
10+
class singleton {
11+
private:
12+
singleton() {}
13+
14+
static singleton *p;
15+
static mutex lock_;
16+
public:
17+
singleton *instance();
18+
19+
// 实现一个内嵌垃圾回收类
20+
class CGarbo
21+
{
22+
public:
23+
~CGarbo()
24+
{
25+
if(singleton::p)
26+
delete singleton::p;
27+
}
28+
};
29+
static CGarbo Garbo; // 定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数从而释放单例对象
30+
};
31+
32+
singleton *singleton::p = nullptr;
33+
singleton::CGarbo Garbo;
34+
35+
singleton* singleton::instance() {
36+
if (p == nullptr) {
37+
lock_guard<mutex> guard(lock_);
38+
if (p == nullptr)
39+
p = new singleton();
40+
}
41+
return p;
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Created by light on 20-2-6.
3+
//
4+
5+
class singleton {
6+
private:
7+
singleton() {}
8+
static singleton *p;
9+
public:
10+
static singleton *instance();
11+
};
12+
13+
singleton *singleton::p = new singleton();
14+
singleton* singleton::instance() {
15+
return p;
16+
}
17+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Created by light on 20-2-6.
3+
//
4+
5+
class singleton {
6+
private:
7+
singleton() {}
8+
static singleton *p;
9+
public:
10+
static singleton *instance();
11+
};
12+
13+
singleton *singleton::p = nullptr;
14+
15+
singleton* singleton::instance() {
16+
if (p == nullptr)
17+
p = new singleton();
18+
return p;
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Created by light on 20-2-7.
3+
//
4+
#include <iostream>
5+
using namespace std;
6+
7+
#include <mutex>
8+
9+
class singleton {
10+
private:
11+
singleton() {}
12+
static singleton *p;
13+
static mutex lock_;
14+
public:
15+
static singleton *instance();
16+
};
17+
18+
singleton *singleton::p = nullptr;
19+
20+
singleton* singleton::instance() {
21+
lock_guard<mutex> guard(lock_);
22+
if (p == nullptr)
23+
p = new singleton();
24+
return p;
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by light on 20-2-6.
3+
//
4+
5+
#include <sys/param.h>
6+
#include <pthread.h>
7+
8+
class singleton {
9+
private:
10+
singleton(); //私有构造函数,不允许使用者自己生成对象
11+
singleton(const singleton &other);
12+
13+
//要写成静态方法的原因:类成员函数隐含传递this指针(第一个参数)
14+
static void init() {
15+
p = new singleton();
16+
}
17+
18+
static pthread_once_t ponce_;
19+
static singleton *p; //静态成员变量
20+
public:
21+
singleton *instance() {
22+
// init函数只会执行一次
23+
pthread_once(&ponce_, &singleton::init);
24+
return p;
25+
}
26+
};
27+
28+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by light on 20-2-7.
3+
//
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
class singleton {
10+
private:
11+
static singleton *p;
12+
singleton() {}
13+
public:
14+
singleton *instance();
15+
};
16+
17+
singleton *singleton::instance() {
18+
static singleton p;
19+
return &p;
20+
}
21+
22+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Created by light on 20-2-7.
3+
//
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
#include <mutex>
9+
10+
#define barrier() __asm__ volatile ("lwsync")
11+
12+
// method 1 operator new + placement new
13+
//singleton *instance() {
14+
// if (p == nullptr) {
15+
// lock_guard<mutex> guard(lock_);
16+
// if (p == nullptr) {
17+
// singleton *tmp = static_cast<singleton *>(operator new(sizeof(singleton)));
18+
// new(p)singleton();
19+
// p = tmp;
20+
// }
21+
// }
22+
// return p;
23+
//}
24+
class singleton {
25+
private:
26+
singleton() {}
27+
28+
static singleton *p;
29+
static mutex lock_;
30+
public:
31+
static singleton *instance();
32+
};
33+
34+
singleton *singleton::p = nullptr;
35+
36+
singleton *singleton::instance() {
37+
if (p == nullptr) {
38+
lock_guard<mutex> guard(lock_);
39+
barrier();
40+
if (p == nullptr) {
41+
p = new singleton();
42+
}
43+
}
44+
return p;
45+
}
46+

0 commit comments

Comments
 (0)