File tree Expand file tree Collapse file tree 17 files changed +505
-10
lines changed
modern_C++_30/memorymodel_atomic Expand file tree Collapse file tree 17 files changed +505
-10
lines changed Original file line number Diff line number Diff line change 137
137
- [myhashtable](./stl_src/myhashtable.md)
138
138
- [unordered_map](./stl_src/unordered_map.md)
139
139
140
- ## 3.多线程与多进程
140
+ ## 3.设计模式
141
141
142
- ### 3.1 Threading In C++
142
+ - [单例模式](./design_pattern/singleton)
143
+
144
+ ## 4.多线程与多进程
145
+
146
+ ### 4.1 Threading In C++
143
147
144
148
- [介绍](./Threading_In_CPlusPlus/1.thread)
145
149
- [创建线程的五种类型](./Threading_In_CPlusPlus/2.create_type)
150
154
>
151
155
> https://www.youtube.com/watch?v=eZ8yKZo-PGw&list=PLk6CEY9XxSIAeK-EAh3hB4fgNvYkYmghp&index=4
152
156
153
- ### 4 .学习课程
157
+ ### 5 .学习课程
154
158
155
- #### 4 .1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
159
+ #### 5 .1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
156
160
157
161
- [堆、栈、RAII:C++里该如何管理资源?](./modern_C++_30/RAII)
158
162
- [堆](./modern_++_30/RAII/heap.cpp)
178
182
- [SFINAE:不是错误的替换失败是怎么回事?](./modern_C++_30/SFINAE)
179
183
- [constexpr:一个常态的世界](./modern_C++_30/constexpr)
180
184
- [函数对象和lambda:进入函数式编程](./modern_C++_30/functionLambda)
185
+ - [内存模型和atomic:理解并发的复杂性](./modern_C++_30/memorymodel_atomic)
181
186
182
- ### 5 .拓展部分
187
+ ### 6 .拓展部分
183
188
184
- #### 5 .1 [C++惯用法](./codingStyleIdioms)
189
+ #### 6 .1 [C++惯用法](./codingStyleIdioms)
185
190
186
191
##### 你最喜欢的c++编程风格惯用法是什么?
187
192
191
196
- [4.copy and swap](./codingStyleIdioms/4_copy-swap)
192
197
- [5.pImpl(指针指向具体实现)](./codingStyleIdioms/5_pImpl)
193
198
194
- #### 5 .2 一些问题
199
+ #### 6 .2 一些问题
195
200
196
201
- [C++中如何将string类型转换为int类型?](./basic_content/extent/string_int.md)
197
202
198
- ### 6 .工具篇
203
+ ### 7 .工具篇
199
204
200
205
- [容器快捷输出工具](./tool/output)
201
206
225
230
226
231
- [https://cppinsights.io](https://cppinsights.io/)
227
232
228
- ### 7 .代码运行
233
+ ### 8 .代码运行
229
234
230
235
- **代码环境**
231
236
235
240
236
241
CLion gcc/g++
237
242
238
- ### 8 .关于作者
243
+ ### 9 .关于作者
239
244
240
245
个人公众号:
241
246
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments