Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-City committed Feb 7, 2020
1 parent 5b5e3bc commit 60af660
Show file tree
Hide file tree
Showing 17 changed files with 505 additions and 10 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@
- [myhashtable](./stl_src/myhashtable.md)
- [unordered_map](./stl_src/unordered_map.md)
## 3.多线程与多进程
## 3.设计模式
### 3.1 Threading In C++
- [单例模式](./design_pattern/singleton)
## 4.多线程与多进程
### 4.1 Threading In C++
- [介绍](./Threading_In_CPlusPlus/1.thread)
- [创建线程的五种类型](./Threading_In_CPlusPlus/2.create_type)
Expand All @@ -150,9 +154,9 @@
>
> https://www.youtube.com/watch?v=eZ8yKZo-PGw&list=PLk6CEY9XxSIAeK-EAh3hB4fgNvYkYmghp&index=4
### 4.学习课程
### 5.学习课程
#### 4.1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
#### 5.1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
- [堆、栈、RAII:C++里该如何管理资源?](./modern_C++_30/RAII)
- [堆](./modern_++_30/RAII/heap.cpp)
Expand All @@ -178,10 +182,11 @@
- [SFINAE:不是错误的替换失败是怎么回事?](./modern_C++_30/SFINAE)
- [constexpr:一个常态的世界](./modern_C++_30/constexpr)
- [函数对象和lambda:进入函数式编程](./modern_C++_30/functionLambda)
- [内存模型和atomic:理解并发的复杂性](./modern_C++_30/memorymodel_atomic)
### 5.拓展部分
### 6.拓展部分
#### 5.1 [C++惯用法](./codingStyleIdioms)
#### 6.1 [C++惯用法](./codingStyleIdioms)
##### 你最喜欢的c++编程风格惯用法是什么?
Expand All @@ -191,11 +196,11 @@
- [4.copy and swap](./codingStyleIdioms/4_copy-swap)
- [5.pImpl(指针指向具体实现)](./codingStyleIdioms/5_pImpl)
#### 5.2 一些问题
#### 6.2 一些问题
- [C++中如何将string类型转换为int类型?](./basic_content/extent/string_int.md)
### 6.工具篇
### 7.工具篇
- [容器快捷输出工具](./tool/output)
Expand Down Expand Up @@ -225,7 +230,7 @@
- [https://cppinsights.io](https://cppinsights.io/)
### 7.代码运行
### 8.代码运行
- **代码环境**
Expand All @@ -235,7 +240,7 @@
CLion gcc/g++
### 8.关于作者
### 9.关于作者
个人公众号:
Expand Down
46 changes: 46 additions & 0 deletions design_pattern/singleton/barrier_singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by light on 20-2-7.
//
#include <iostream>

using namespace std;

#include <mutex>

#define barrier() __asm__ volatile ("lwsync")

// method 1 operator new + placement new
//singleton *instance() {
// if (p == nullptr) {
// lock_guard<mutex> guard(lock_);
// if (p == nullptr) {
// singleton *tmp = static_cast<singleton *>(operator new(sizeof(singleton)));
// new(p)singleton();
// p = tmp;
// }
// }
// return p;
//}
class singleton {
private:
singleton() {}

static singleton *p;
static mutex lock_;
public:
static singleton *instance();
};

singleton *singleton::p = nullptr;

singleton *singleton::instance() {
if (p == nullptr) {
lock_guard<mutex> guard(lock_);
barrier();
if (p == nullptr) {
p = new singleton();
}
}
return p;
}

46 changes: 46 additions & 0 deletions design_pattern/singleton/cpulpuls11_singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by light on 20-2-7.
//

#include <iostream>

using namespace std;

#include <mutex>
#include <atomic>

//C++ 11版本之后的跨平台实现
class singleton {
private:
singleton() {}

static mutex lock_;
static atomic<singleton *> p;
public:
singleton *instance();
};

mutex singleton::lock_;
atomic<singleton *> 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<mutex> 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;
}


42 changes: 42 additions & 0 deletions design_pattern/singleton/dcl_singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Created by light on 20-2-7.
//
#include <iostream>

using namespace std;

#include <mutex>

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<mutex> guard(lock_);
if (p == nullptr)
p = new singleton();
}
return p;
}
17 changes: 17 additions & 0 deletions design_pattern/singleton/hungrysingleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by light on 20-2-6.
//

class singleton {
private:
singleton() {}
static singleton *p;
public:
static singleton *instance();
};

singleton *singleton::p = new singleton();
singleton* singleton::instance() {
return p;
}

19 changes: 19 additions & 0 deletions design_pattern/singleton/iazysingleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by light on 20-2-6.
//

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;
}
25 changes: 25 additions & 0 deletions design_pattern/singleton/lock_singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Created by light on 20-2-7.
//
#include <iostream>
using namespace std;

#include <mutex>

class singleton {
private:
singleton() {}
static singleton *p;
static mutex lock_;
public:
static singleton *instance();
};

singleton *singleton::p = nullptr;

singleton* singleton::instance() {
lock_guard<mutex> guard(lock_);
if (p == nullptr)
p = new singleton();
return p;
}
28 changes: 28 additions & 0 deletions design_pattern/singleton/pthreadoncesingleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Created by light on 20-2-6.
//

#include <sys/param.h>
#include <pthread.h>

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;
}
};


22 changes: 22 additions & 0 deletions design_pattern/singleton/static_local_singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by light on 20-2-7.
//

#include <iostream>

using namespace std;

class singleton {
private:
static singleton *p;
singleton() {}
public:
singleton *instance();
};

singleton *singleton::instance() {
static singleton p;
return &p;
}


46 changes: 46 additions & 0 deletions modern_C++_30/memorymodel_atomic/barrier_singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by light on 20-2-7.
//
#include <iostream>

using namespace std;

#include <mutex>

#define barrier() __asm__ volatile ("lwsync")

// method 1 operator new + placement new
//singleton *instance() {
// if (p == nullptr) {
// lock_guard<mutex> guard(lock_);
// if (p == nullptr) {
// singleton *tmp = static_cast<singleton *>(operator new(sizeof(singleton)));
// new(p)singleton();
// p = tmp;
// }
// }
// return p;
//}
class singleton {
private:
singleton() {}

static singleton *p;
static mutex lock_;
public:
static singleton *instance();
};

singleton *singleton::p = nullptr;

singleton *singleton::instance() {
if (p == nullptr) {
lock_guard<mutex> guard(lock_);
barrier();
if (p == nullptr) {
p = new singleton();
}
}
return p;
}

Loading

0 comments on commit 60af660

Please sign in to comment.