From 16c12a3bc6a1868ee6098d8f0646e64f7438dd30 Mon Sep 17 00:00:00 2001 From: Light-City <455954986@qq.com> Date: Tue, 3 Mar 2020 10:00:10 +0800 Subject: [PATCH] update --- README.md | 44 +-- .../.idea/Threading_In_CPlusPlus.iml | 0 .../Threading_In_CPlusPlus}/.idea/misc.xml | 0 .../Threading_In_CPlusPlus}/.idea/modules.xml | 0 .../.idea/workspace.xml | 0 .../1.thread/intro.cpp | 0 .../1.thread/thread.cpp | 0 .../2.create_type/1.function_pointer.cpp | 0 .../2.create_type/2.lambda_function.cpp | 0 .../2.create_type/3.functor.cpp | 0 .../4.no_static_member_function.cpp | 0 .../5.static_member_function.cpp | 0 .../3.join_detach/detach.cpp | 0 .../3.join_detach/join.cpp | 0 .../4.mutex/critical_section.cpp | 0 .../Threading_In_CPlusPlus}/CMakeLists.txt | 0 .../concurrency_v1}/chapter1/1_helloworld.cpp | 0 .../concurrency_v1}/chapter2/2.1_basic.cpp | 0 .../concurrency_v1}/chapter2/2.2_transfer.cpp | 0 .../chapter2/2.3_ownership.cpp | 0 .../concurrency_v1}/chapter2/2.4_runtime.cpp | 0 .../concurrency_v1}/chapter2/2_5_id.cpp | 0 design_pattern/singleton/README.md | 327 ++++++++++++++++++ {stl => src_analysis/stl}/array.md | 0 {stl => src_analysis/stl}/deque.md | 0 {stl => src_analysis/stl}/hashtable.md | 0 {stl => src_analysis/stl}/iterator.md | 0 {stl => src_analysis/stl}/list.md | 0 {stl => src_analysis/stl}/map_multimap.md | 0 {stl => src_analysis/stl}/myhashtable.md | 0 {stl => src_analysis/stl}/queue_stack.md | 0 {stl => src_analysis/stl}/rb_tree.md | 0 {stl => src_analysis/stl}/set_multiset.md | 0 {stl => src_analysis/stl}/traits.md | 0 {stl => src_analysis/stl}/typename.md | 0 {stl => src_analysis/stl}/unordered_map.md | 0 {stl => src_analysis/stl}/vector.md | 0 .../stl}/谈谈STL设计之EBO优化.md | 0 38 files changed, 349 insertions(+), 22 deletions(-) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/.idea/Threading_In_CPlusPlus.iml (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/.idea/misc.xml (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/.idea/modules.xml (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/.idea/workspace.xml (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/1.thread/intro.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/1.thread/thread.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/2.create_type/1.function_pointer.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/2.create_type/2.lambda_function.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/2.create_type/3.functor.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/2.create_type/4.no_static_member_function.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/2.create_type/5.static_member_function.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/3.join_detach/detach.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/3.join_detach/join.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/4.mutex/critical_section.cpp (100%) rename {Threading_In_CPlusPlus => concurrency/Threading_In_CPlusPlus}/CMakeLists.txt (100%) rename {concurrency_v1 => concurrency/concurrency_v1}/chapter1/1_helloworld.cpp (100%) rename {concurrency_v1 => concurrency/concurrency_v1}/chapter2/2.1_basic.cpp (100%) rename {concurrency_v1 => concurrency/concurrency_v1}/chapter2/2.2_transfer.cpp (100%) rename {concurrency_v1 => concurrency/concurrency_v1}/chapter2/2.3_ownership.cpp (100%) rename {concurrency_v1 => concurrency/concurrency_v1}/chapter2/2.4_runtime.cpp (100%) rename {concurrency_v1 => concurrency/concurrency_v1}/chapter2/2_5_id.cpp (100%) create mode 100644 design_pattern/singleton/README.md rename {stl => src_analysis/stl}/array.md (100%) rename {stl => src_analysis/stl}/deque.md (100%) rename {stl => src_analysis/stl}/hashtable.md (100%) rename {stl => src_analysis/stl}/iterator.md (100%) rename {stl => src_analysis/stl}/list.md (100%) rename {stl => src_analysis/stl}/map_multimap.md (100%) rename {stl => src_analysis/stl}/myhashtable.md (100%) rename {stl => src_analysis/stl}/queue_stack.md (100%) rename {stl => src_analysis/stl}/rb_tree.md (100%) rename {stl => src_analysis/stl}/set_multiset.md (100%) rename {stl => src_analysis/stl}/traits.md (100%) rename {stl => src_analysis/stl}/typename.md (100%) rename {stl => src_analysis/stl}/unordered_map.md (100%) rename {stl => src_analysis/stl}/vector.md (100%) rename {stl => src_analysis/stl}/谈谈STL设计之EBO优化.md (100%) diff --git a/README.md b/README.md index 1ef8e99..bf1a201 100644 --- a/README.md +++ b/README.md @@ -120,32 +120,32 @@ C++2.0简称包括C++11/14/17/20。 - [单例模式](./design_pattern/singleton) -### 4. [STL源码剖析](./stl_src) +### 4. [STL源码剖析](./src_analysis/stl) **stl源码剖析:gcc4.9.1** -- [array](./stl_src/array.md) -- [deque](./stl_src/deque.md) -- [queue and stack](./stl_src/queue_stack.md) -- [list](./stl_src/list.md) -- [vector](./stl_src/vector.md) -- [typename](./stl_src/typename.md) -- [traits](./stl_src/traits.md) -- [iterator](./stl_src/iterator.md) -- [谈谈STL设计之EBO优化](./stl_src/谈谈STL设计之EBO优化.md) -- [rb_tree](./stl_src/rb_tree.md) +- [array](./src_analysis/stl/array.md) +- [deque](./src_analysis/stl/deque.md) +- [queue and stack](./src_analysis/stl/queue_stack.md) +- [list](./src_analysis/stl/list.md) +- [vector](./src_analysis/stl/vector.md) +- [typename](./src_analysis/stl/typename.md) +- [traits](./src_analysis/stl/traits.md) +- [iterator](./src_analysis/stl/iterator.md) +- [谈谈STL设计之EBO优化](./src_analysis/stl/谈谈STL设计之EBO优化.md) +- [rb_tree](./src_analysis/stl/rb_tree.md) - [set and multiset](set_multiset.md) -- [map and multimap](./stl_src/map_multimap.md) -- [hashtable](./stl_src/hashtable.md) -- [myhashtable](./stl_src/myhashtable.md) -- [unordered_map](./stl_src/unordered_map.md) +- [map and multimap](./src_analysis/stl/map_multimap.md) +- [hashtable](./src_analysis/stl/hashtable.md) +- [myhashtable](./src_analysis/stl/myhashtable.md) +- [unordered_map](./src_analysis/stl/unordered_map.md) -### 4. [并发编程](./c++2.0/./concurrency_v1) +### 4. [并发编程](./concurrency) #### 4.1 C++ Concurrency in Action -- [第一章](./c++2.0/./concurrency_v1/chapter1) -- [第二章](./c++2.0/./concurrency_v1/chapter2) +- [第一章](./concurrency/concurrency_v1/chapter1) +- [第二章](./concurrency/concurrency_v1/chapter2) 学习资料:https://chenxiaowei.gitbook.io/cpp_concurrency_in_action/ @@ -153,10 +153,10 @@ C++2.0简称包括C++11/14/17/20。 ##### 4.2.1 Threading In C++ -- [介绍](./Threading_In_CPlusPlus/1.thread) -- [创建线程的五种类型](./Threading_In_CPlusPlus/2.create_type) -- [Join与Detachs](./Threading_In_CPlusPlus/3.join_detach) -- [mutex in C++ Threading](./Threading_In_CPlusPlus/4.mutex) +- [介绍](./concurrency/Threading_In_CPlusPlus/1.thread) +- [创建线程的五种类型](./concurrency/Threading_In_CPlusPlus/2.create_type) +- [Join与Detachs](./concurrency/Threading_In_CPlusPlus/3.join_detach) +- [mutex in C++ Threading](./concurrency/Threading_In_CPlusPlus/4.mutex) > 学习自: > diff --git a/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml b/concurrency/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml similarity index 100% rename from Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml rename to concurrency/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml diff --git a/Threading_In_CPlusPlus/.idea/misc.xml b/concurrency/Threading_In_CPlusPlus/.idea/misc.xml similarity index 100% rename from Threading_In_CPlusPlus/.idea/misc.xml rename to concurrency/Threading_In_CPlusPlus/.idea/misc.xml diff --git a/Threading_In_CPlusPlus/.idea/modules.xml b/concurrency/Threading_In_CPlusPlus/.idea/modules.xml similarity index 100% rename from Threading_In_CPlusPlus/.idea/modules.xml rename to concurrency/Threading_In_CPlusPlus/.idea/modules.xml diff --git a/Threading_In_CPlusPlus/.idea/workspace.xml b/concurrency/Threading_In_CPlusPlus/.idea/workspace.xml similarity index 100% rename from Threading_In_CPlusPlus/.idea/workspace.xml rename to concurrency/Threading_In_CPlusPlus/.idea/workspace.xml diff --git a/Threading_In_CPlusPlus/1.thread/intro.cpp b/concurrency/Threading_In_CPlusPlus/1.thread/intro.cpp similarity index 100% rename from Threading_In_CPlusPlus/1.thread/intro.cpp rename to concurrency/Threading_In_CPlusPlus/1.thread/intro.cpp diff --git a/Threading_In_CPlusPlus/1.thread/thread.cpp b/concurrency/Threading_In_CPlusPlus/1.thread/thread.cpp similarity index 100% rename from Threading_In_CPlusPlus/1.thread/thread.cpp rename to concurrency/Threading_In_CPlusPlus/1.thread/thread.cpp diff --git a/Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp b/concurrency/Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp similarity index 100% rename from Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp rename to concurrency/Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp diff --git a/Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp b/concurrency/Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp similarity index 100% rename from Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp rename to concurrency/Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp diff --git a/Threading_In_CPlusPlus/2.create_type/3.functor.cpp b/concurrency/Threading_In_CPlusPlus/2.create_type/3.functor.cpp similarity index 100% rename from Threading_In_CPlusPlus/2.create_type/3.functor.cpp rename to concurrency/Threading_In_CPlusPlus/2.create_type/3.functor.cpp diff --git a/Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp b/concurrency/Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp similarity index 100% rename from Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp rename to concurrency/Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp diff --git a/Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp b/concurrency/Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp similarity index 100% rename from Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp rename to concurrency/Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp diff --git a/Threading_In_CPlusPlus/3.join_detach/detach.cpp b/concurrency/Threading_In_CPlusPlus/3.join_detach/detach.cpp similarity index 100% rename from Threading_In_CPlusPlus/3.join_detach/detach.cpp rename to concurrency/Threading_In_CPlusPlus/3.join_detach/detach.cpp diff --git a/Threading_In_CPlusPlus/3.join_detach/join.cpp b/concurrency/Threading_In_CPlusPlus/3.join_detach/join.cpp similarity index 100% rename from Threading_In_CPlusPlus/3.join_detach/join.cpp rename to concurrency/Threading_In_CPlusPlus/3.join_detach/join.cpp diff --git a/Threading_In_CPlusPlus/4.mutex/critical_section.cpp b/concurrency/Threading_In_CPlusPlus/4.mutex/critical_section.cpp similarity index 100% rename from Threading_In_CPlusPlus/4.mutex/critical_section.cpp rename to concurrency/Threading_In_CPlusPlus/4.mutex/critical_section.cpp diff --git a/Threading_In_CPlusPlus/CMakeLists.txt b/concurrency/Threading_In_CPlusPlus/CMakeLists.txt similarity index 100% rename from Threading_In_CPlusPlus/CMakeLists.txt rename to concurrency/Threading_In_CPlusPlus/CMakeLists.txt diff --git a/concurrency_v1/chapter1/1_helloworld.cpp b/concurrency/concurrency_v1/chapter1/1_helloworld.cpp similarity index 100% rename from concurrency_v1/chapter1/1_helloworld.cpp rename to concurrency/concurrency_v1/chapter1/1_helloworld.cpp diff --git a/concurrency_v1/chapter2/2.1_basic.cpp b/concurrency/concurrency_v1/chapter2/2.1_basic.cpp similarity index 100% rename from concurrency_v1/chapter2/2.1_basic.cpp rename to concurrency/concurrency_v1/chapter2/2.1_basic.cpp diff --git a/concurrency_v1/chapter2/2.2_transfer.cpp b/concurrency/concurrency_v1/chapter2/2.2_transfer.cpp similarity index 100% rename from concurrency_v1/chapter2/2.2_transfer.cpp rename to concurrency/concurrency_v1/chapter2/2.2_transfer.cpp diff --git a/concurrency_v1/chapter2/2.3_ownership.cpp b/concurrency/concurrency_v1/chapter2/2.3_ownership.cpp similarity index 100% rename from concurrency_v1/chapter2/2.3_ownership.cpp rename to concurrency/concurrency_v1/chapter2/2.3_ownership.cpp diff --git a/concurrency_v1/chapter2/2.4_runtime.cpp b/concurrency/concurrency_v1/chapter2/2.4_runtime.cpp similarity index 100% rename from concurrency_v1/chapter2/2.4_runtime.cpp rename to concurrency/concurrency_v1/chapter2/2.4_runtime.cpp diff --git a/concurrency_v1/chapter2/2_5_id.cpp b/concurrency/concurrency_v1/chapter2/2_5_id.cpp similarity index 100% rename from concurrency_v1/chapter2/2_5_id.cpp rename to concurrency/concurrency_v1/chapter2/2_5_id.cpp diff --git a/design_pattern/singleton/README.md b/design_pattern/singleton/README.md new file mode 100644 index 0000000..f593319 --- /dev/null +++ b/design_pattern/singleton/README.md @@ -0,0 +1,327 @@ +## C++设计模式之单例模式 + +## 0.导语 + +相信大家面试都逃不开设计模式话题,本节将阐述面试中的最常用的设计模式(单例模式),从分类,线程安全,不基于C++11标准的角度与基于C++11标准的角度,有哪些解决线程安全的单例模式方案,相信认真看完本篇文章,在以后面试中就不用担忧了。 + +## 1.众所周知的单例 + +在一般书籍中或者大家比较是熟知的单例模式是下面这样: + +```cpp +class singleton { +private: + singleton() {} + static singleton *p; +public: + static singleton *instance(); +}; + +singleton *singleton::p = nullptr; + +singleton* singleton::instance() { + if (p == nullptr) + p = new singleton(); + return p; +} +``` + +这是一个非常简单的实现,将构造函数声明为private或protect防止被外部函数实例化,内部有一个静态的类指针保存唯一的实例,实例的实现由一个public方法来实现,该方法返回该类的唯一实例。 + +当然这个代码只适合在单线程下,当多线程时,是不安全的。考虑两个线程同时首次调用instance方法且同时检测到p是nullptr,则两个线程会同时构造一个实例给p,这将违反了单例的准则。 + +## 2.懒汉与饿汉 + +单例分为两种实现方法: + +- 懒汉 + - 第一次用到类实例的时候才会去实例化,上述就是懒汉实现。 +- 饿汉 + - 单例类定义的时候就进行了实例化。 + +这里也给出饿汉的实现: + +```cpp +class singleton { +private: + singleton() {} + static singleton *p; +public: + static singleton *instance(); +}; + +singleton *singleton::p = new singleton(); +singleton* singleton::instance() { + return p; +} +``` + +当然这个是线程安全的,对于我们通常阐述的线程不安全,为懒汉模式,下面会阐述懒汉模式的线程安全代码优化。 + +## 3.多线程加锁 + +在C++中加锁有个类实现原理采用RAII,不用手动管理unlock,那就是lock_guard,这里采用其进行加锁。 + +```cpp +class singleton { +private: + singleton() {} + static singleton *p; + static mutex lock_; +public: + static singleton *instance(); +}; + +singleton *singleton::p = nullptr; + +singleton* singleton::instance() { + lock_guard guard(lock_); + if (p == nullptr) + p = new singleton(); + return p; +} +``` + +这种写法不会出现上面两个线程都执行到p=nullptr里面的情况,当线程A在执行`p = new Singleton()`的时候,线程B如果调用了`instance()`,一定会被阻塞在加锁处,等待线程A执行结束后释放这个锁。从而是线程安全的。 + +但是这种写法性能非常低下,因为每次调用`instance()`都会加锁释放锁,而这个步骤只有在第一次`new Singleton()`才是有必要的,只要`p`被创建出来了,不管多少线程同时访问,使用`if (p == nullptr) `进行判断都是足够的(只是读操作,不需要加锁),没有线程安全问题,加了锁之后反而存在性能问题。 + +因此引出DCL。 + +## 4.双重检查锁模式 + +上面写法是不管任何情况都会去加锁,然后释放锁,而对于读操作是不存在线程安全的,故只需要在第一次实例创建的时候加锁,以后不需要。下面先看一下DCLP的实现: + +```cpp +singleton* singleton::instance() { + if(p == nullptr) { // 第一次检查 + Lock lock; + if(p == nullptr){ // 第二次检查 + p = new singleton; + } + } + return p; +} +``` + +基于上述,我们可以写出双重检查锁+自动回收 + +```cpp +class singleton { +private: + singleton() {} + + static singleton *p; + static mutex lock_; +public: + singleton *instance(); + + // 实现一个内嵌垃圾回收类 + class CGarbo + { + public: + ~CGarbo() + { + if(singleton::p) + delete singleton::p; + } + }; + static CGarbo Garbo; // 定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数从而释放单例对象 +}; + +singleton *singleton::p = nullptr; +singleton::CGarbo Garbo; + +singleton* singleton::instance() { + if (p == nullptr) { + lock_guard guard(lock_); + if (p == nullptr) + p = new singleton(); + } + return p; +} + +``` + +DCLP的关键在于,大多数对instance的调用会看到p是非空的,因此甚至不用尝试去初始化它。因此,DCLP在尝试获取锁之前检查p是否为空。只有当检查成功(也就是p还没有被初始化)时才会去获得锁,然后再次检查p是否仍然为空(因此命名为双重检查锁)。**第二次检查是必要,因为就像我们刚刚看到的,很有可能另一个线程偶然在第一次检查之后,获得锁成功之前初始化p。** + +看起来上述代码非常美好,可是过了相当一段时间后,才发现这个漏洞,原因是:**内存读写的乱序执行(编译器问题)**。 + +再次考虑初始化`p`的那一行: + +```cpp +p = new singleton; +``` + +这条语句会导致三个事情的发生: + +1. 分配能够存储`singleton`对象的内存; +2. 在被分配的内存中构造一个`singleton`对象; +3. 让`p`指向这块被分配的内存。 + +可能会认为这三个步骤是按顺序执行的,但实际上只能确定步骤`1`是最先执行的,步骤`2`,`3`却不一定。问题就出现在这。 + +- 线程A调用instance,执行第一次p的测试,获得锁,按照1,3,执行,然后被挂起。此时p是非空的,但是p指向的内存中还没有Singleton对象被构造。 +- 线程B调用instance,判定p非空, 将其返回给instance的调用者。调用者对指针解引用以获得singleton,噢,一个还没有被构造出的对象。bug就出现了。 + +DCLP能够良好的工作仅当步骤一和二在步骤三之前被执行,但是并没有并没有方法在C或C++中表达这种限制。这就像是插在DCLP心脏上的一把匕首:我们需要在相对指令顺序上定义限制,但是我们的语言没有给出表达这种限制的方法。 + +## 5.memory barrier指令 + +DCLP问题在C++11中,这个问题得到了解决。 + +因为新的C++11规定了新的内存模型,保证了执行上述3个步骤的时候不会发生线程切换,相当这个初始化过程是“原子性”的的操作,DCL又可以正确使用了,不过在C++11下却有更简洁的多线程singleton写法了,这个留在后面再介绍。 + +C++11之前解决方法是barrier指令。要使其正确执行的话,就得在步骤2、3直接加上一道memory barrier。强迫CPU执行的时候按照1、2、3的步骤来运行。 + +**第一种实现:** + +基于operator new+placement new,遵循1,2,3执行顺序依次编写代码。 + +``` +// method 1 operator new + placement new +singleton *instance() { + if (p == nullptr) { + lock_guard guard(lock_); + if (p == nullptr) { + singleton *tmp = static_cast(operator new(sizeof(singleton))); + new(p)singleton(); + p = tmp; + } + } + return p; +} +``` + +**第二种实现:** + +基于直接嵌入ASM汇编指令mfence,uninx的barrier宏也是通过该指令实现的。 + +```cpp +#define barrier() __asm__ volatile ("lwsync") +singleton *singleton::instance() { + if (p == nullptr) { + lock_guard guard(lock_); + barrier(); + if (p == nullptr) { + p = new singleton(); + } + } + return p; +} +``` + +通常情况下是调用cpu提供的一条指令,这条指令的作用是会阻止cpu将该指令之前的指令交换到该指令之后,这条指令也通常被叫做barrier。 上面代码中的**asm**表示这个是一条汇编指令,volatile是可选的,如果用了它,则表示向编译器声明不允许对该汇编指令进行优化。lwsync是POWERPC提供的barrier指令。 + +## 6.静态局部变量 + +Scott Meyer在《Effective C++》中提出了一种简洁的singleton写法 + +```cpp +singleton *singleton::instance() { + static singleton p; + return &p; +} +``` + +- 单线程下,正确。 +- C++11及以后的版本(如C++14)的多线程下,正确。 +- C++11之前的多线程下,不一定正确。 + +原因在于在C++11之前的标准中并没有规定local static变量的内存模型。于是乎它就是不是线程安全的了。但是在C++11却是线程安全的,这是因为新的C++标准规定了当一个线程正在初始化一个变量的时候,其他线程必须得等到该初始化完成以后才能访问它。 + +上述使用的内存序: + +- memory_order_relaxed:松散内存序,只用来保证对原子对象的操作是原子的 +- memory_order_acquire:获得操作,在读取某原子对象时,当前线程的任何后面的读写操作都不允许重排到这个操作的前面去,并且其他线程在对同一个原子对象释放之前的所有内存写入都在当前线程可见 +- memory_order_release:释放操作,在写入某原子对象时,当前线程的任何前面的读写操作都不允许重排到这个操作的后面去,并且当前线程的所有内存写入都在对同一个原子对象进行获取的其他线程可见 + +## 7.Atomic + +在C++11之前的版本下,除了通过锁实现线程安全的Singleton外,还可以利用各个编译器内置的atomic operation来实现。 + +`java`和`c#`发现乱序问题后,就加了一个关键字`volatile`,在声明`p`变量的时候,要加上`volatile`修饰,编译器看到之后,就知道这个地方不能够reorder(一定要先分配内存,在执行构造器,都完成之后再赋值)。 + +而对于`c++`标准却一直没有改正,所以`VC++`在`2005`版本也加入了这个关键字,但是这并不能够跨平台(只支持微软平台)。 + +而到了`c++ 11`版本,为了从根本上消除这些漏洞,引入了适合多线程的内存模型。终于有了这样的机制帮助我们实现跨平台的方案。 + +```cpp +mutex singleton::lock_; +atomic singleton::p; + +/* +* std::atomic_thread_fence(std::memory_order_acquire); +* std::atomic_thread_fence(std::memory_order_release); +* 这两句话可以保证他们之间的语句不会发生乱序执行。 +*/ +singleton *singleton::instance() { + singleton *tmp = p.load(memory_order_relaxed); + atomic_thread_fence(memory_order_acquire); + if (tmp == nullptr) { + lock_guard guard(lock_); + tmp = p.load(memory_order_relaxed); + if (tmp == nullptr) { + tmp = new singleton(); + atomic_thread_fence(memory_order_release); + p.store(tmp, memory_order_relaxed); + } + } + return p; +} +``` + +值得注意的是,上述代码使用两个比较关键的术语,获得与释放: + +- 获得是一个对内存的读操作,当前线程的任何后面的读写操作都不允许重排到这个操作的前面去。 +- 释放是一个对内存的写操作,当前线程的任何前面的读写操作都不允许重排到这个操作的后面去。 + +acquire 和 release 通常都是配对出现的,目的是保证如果对同一个原子对象的 release 发生在 acquire 之前的话,release 之前发生的内存修改能够被 acquire 之后的内存读取全部看到。 + +## 8.pthread_once + +如果是在unix平台的话,除了使用atomic operation外,在不适用C++11的情况下,还可以通过pthread_once来实现Singleton。 + +原型如下: + +``` +int pthread_once(pthread_once_t once_control, void (init_routine) (void)); +``` + +实现: + +```cpp +class singleton { +private: + singleton(); //私有构造函数,不允许使用者自己生成对象 + singleton(const singleton &other); + + //要写成静态方法的原因:类成员函数隐含传递this指针(第一个参数) + static void init() { + p = new singleton(); + } + + static pthread_once_t ponce_; + static singleton *p; //静态成员变量 +public: + singleton *instance() { + // init函数只会执行一次 + pthread_once(&ponce_, &singleton::init); + return p; + } +}; +``` + +## 9.总结 + +本文讲解了几种单例模式,并讲解了线程安全的单例模式,以及不用C++11实现的几种线程安全的单例模式:memory barrier,静态局部变量,pthread_once方式,C++11的atomic实现等。 + +最后值得注意的是,针对上述单例类的析构函数请参考双重锁检查模式+自动回收实现,必须在类中声明一个静态局部变量,静态局部变量可以理解为全局变量,在程序结束时,自动调用该静态局部变量的析构函数,这就是为什么要在类中声明与定义嵌套类,而不是直接编写单例的析构函数。 + +## 10.参考资料 + +> https://thorns.cn/2019/03/09/DCLP.html + +> https://www.cnblogs.com/liyuan989/p/4264889.html + +> https://segmentfault.com/a/1190000015950693 \ No newline at end of file diff --git a/stl/array.md b/src_analysis/stl/array.md similarity index 100% rename from stl/array.md rename to src_analysis/stl/array.md diff --git a/stl/deque.md b/src_analysis/stl/deque.md similarity index 100% rename from stl/deque.md rename to src_analysis/stl/deque.md diff --git a/stl/hashtable.md b/src_analysis/stl/hashtable.md similarity index 100% rename from stl/hashtable.md rename to src_analysis/stl/hashtable.md diff --git a/stl/iterator.md b/src_analysis/stl/iterator.md similarity index 100% rename from stl/iterator.md rename to src_analysis/stl/iterator.md diff --git a/stl/list.md b/src_analysis/stl/list.md similarity index 100% rename from stl/list.md rename to src_analysis/stl/list.md diff --git a/stl/map_multimap.md b/src_analysis/stl/map_multimap.md similarity index 100% rename from stl/map_multimap.md rename to src_analysis/stl/map_multimap.md diff --git a/stl/myhashtable.md b/src_analysis/stl/myhashtable.md similarity index 100% rename from stl/myhashtable.md rename to src_analysis/stl/myhashtable.md diff --git a/stl/queue_stack.md b/src_analysis/stl/queue_stack.md similarity index 100% rename from stl/queue_stack.md rename to src_analysis/stl/queue_stack.md diff --git a/stl/rb_tree.md b/src_analysis/stl/rb_tree.md similarity index 100% rename from stl/rb_tree.md rename to src_analysis/stl/rb_tree.md diff --git a/stl/set_multiset.md b/src_analysis/stl/set_multiset.md similarity index 100% rename from stl/set_multiset.md rename to src_analysis/stl/set_multiset.md diff --git a/stl/traits.md b/src_analysis/stl/traits.md similarity index 100% rename from stl/traits.md rename to src_analysis/stl/traits.md diff --git a/stl/typename.md b/src_analysis/stl/typename.md similarity index 100% rename from stl/typename.md rename to src_analysis/stl/typename.md diff --git a/stl/unordered_map.md b/src_analysis/stl/unordered_map.md similarity index 100% rename from stl/unordered_map.md rename to src_analysis/stl/unordered_map.md diff --git a/stl/vector.md b/src_analysis/stl/vector.md similarity index 100% rename from stl/vector.md rename to src_analysis/stl/vector.md diff --git a/stl/谈谈STL设计之EBO优化.md b/src_analysis/stl/谈谈STL设计之EBO优化.md similarity index 100% rename from stl/谈谈STL设计之EBO优化.md rename to src_analysis/stl/谈谈STL设计之EBO优化.md