From 53188690251b5724dd990e5e9eae4ff2e6dcde46 Mon Sep 17 00:00:00 2001
From: Light-City <455954986@qq.com>
Date: Sat, 1 Feb 2020 09:20:20 +0800
Subject: [PATCH] update
---
README.md | 39 +-
.../.idea/Threading_In_CPlusPlus.iml | 2 +
Threading_In_CPlusPlus/.idea/misc.xml | 7 +
Threading_In_CPlusPlus/.idea/modules.xml | 8 +
Threading_In_CPlusPlus/.idea/workspace.xml | 510 ++++++++++++++++++
Threading_In_CPlusPlus/1.thread/intro.cpp | 41 ++
Threading_In_CPlusPlus/1.thread/thread.cpp | 56 ++
.../2.create_type/1.function_pointer.cpp | 23 +
.../2.create_type/2.lambda_function.cpp | 31 ++
.../2.create_type/3.functor.cpp | 25 +
.../4.no_static_member_function.cpp | 26 +
.../5.static_member_function.cpp | 25 +
.../3.join_detach/detach.cpp | 34 ++
Threading_In_CPlusPlus/3.join_detach/join.cpp | 35 ++
.../4.mutex/critical_section.cpp | 34 ++
Threading_In_CPlusPlus/CMakeLists.txt | 16 +
16 files changed, 900 insertions(+), 12 deletions(-)
create mode 100644 Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml
create mode 100644 Threading_In_CPlusPlus/.idea/misc.xml
create mode 100644 Threading_In_CPlusPlus/.idea/modules.xml
create mode 100644 Threading_In_CPlusPlus/.idea/workspace.xml
create mode 100644 Threading_In_CPlusPlus/1.thread/intro.cpp
create mode 100644 Threading_In_CPlusPlus/1.thread/thread.cpp
create mode 100644 Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp
create mode 100644 Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp
create mode 100644 Threading_In_CPlusPlus/2.create_type/3.functor.cpp
create mode 100644 Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp
create mode 100644 Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp
create mode 100644 Threading_In_CPlusPlus/3.join_detach/detach.cpp
create mode 100644 Threading_In_CPlusPlus/3.join_detach/join.cpp
create mode 100644 Threading_In_CPlusPlus/4.mutex/critical_section.cpp
create mode 100644 Threading_In_CPlusPlus/CMakeLists.txt
diff --git a/README.md b/README.md
index 6612bc4..ac8f1c6 100644
--- a/README.md
+++ b/README.md
@@ -137,9 +137,22 @@
- [myhashtable](./stl_src/myhashtable.md)
- [unordered_map](./stl_src/unordered_map.md)
-### 3.学习课程
+## 3.多线程与多进程
-#### 3.1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
+### 3.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)
+
+> 学习自:
+>
+> https://www.youtube.com/watch?v=eZ8yKZo-PGw&list=PLk6CEY9XxSIAeK-EAh3hB4fgNvYkYmghp&index=4
+
+### 4.学习课程
+
+#### 4.1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home)
- [堆、栈、RAII:C++里该如何管理资源?](./modern_C++_30/RAII)
- [堆](./modern_++_30/RAII/heap.cpp)
@@ -166,9 +179,9 @@
- [constexpr:一个常态的世界](./modern_C++_30/constexpr)
- [函数对象和lambda:进入函数式编程](./modern_C++_30/functionLambda)
-### 4.拓展部分
+### 5.拓展部分
-#### 4.1 [C++惯用法](./codingStyleIdioms)
+#### 5.1 [C++惯用法](./codingStyleIdioms)
##### 你最喜欢的c++编程风格惯用法是什么?
@@ -178,11 +191,11 @@
- [4.copy and swap](./codingStyleIdioms/4_copy-swap)
- [5.pImpl(指针指向具体实现)](./codingStyleIdioms/5_pImpl)
-#### 4.2 一些问题
+#### 5.2 一些问题
- [C++中如何将string类型转换为int类型?](./basic_content/extent/string_int.md)
-### 5.工具篇
+### 6.工具篇
- [容器快捷输出工具](./tool/output)
@@ -205,22 +218,24 @@
```
- 像Python一样简单输出
- - [像Python一样玩C/C++](./tool/像Python一样玩CC++.md)
-
+
+- [像Python一样玩C/C++](./tool/像Python一样玩CC++.md)
+
- 观察编译过程变化
+
- [https://cppinsights.io](https://cppinsights.io/)
-### 6.代码运行
+### 7.代码运行
- **代码环境**
-Ubuntu 18.04
+ Ubuntu 18.04
- **工具**
-CLion gcc/g++
+ CLion gcc/g++
-### 7.关于作者
+### 8.关于作者
个人公众号:
diff --git a/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml b/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml
new file mode 100644
index 0000000..f08604b
--- /dev/null
+++ b/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/.idea/misc.xml b/Threading_In_CPlusPlus/.idea/misc.xml
new file mode 100644
index 0000000..8822db8
--- /dev/null
+++ b/Threading_In_CPlusPlus/.idea/misc.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/.idea/modules.xml b/Threading_In_CPlusPlus/.idea/modules.xml
new file mode 100644
index 0000000..93828d7
--- /dev/null
+++ b/Threading_In_CPlusPlus/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/.idea/workspace.xml b/Threading_In_CPlusPlus/.idea/workspace.xml
new file mode 100644
index 0000000..648a701
--- /dev/null
+++ b/Threading_In_CPlusPlus/.idea/workspace.xml
@@ -0,0 +1,510 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1580451516661
+
+
+ 1580451516661
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/1.thread/intro.cpp b/Threading_In_CPlusPlus/1.thread/intro.cpp
new file mode 100644
index 0000000..35d8485
--- /dev/null
+++ b/Threading_In_CPlusPlus/1.thread/intro.cpp
@@ -0,0 +1,41 @@
+//
+// Created by light on 20-1-31.
+//
+
+#include
+#include
+
+using namespace std::chrono;
+using namespace std;
+
+using ull = unsigned long long;
+ull OddSum = 0;
+ull EvenSum = 0;
+
+void findEven(ull start, ull end) {
+ for (ull i = start; i <= end; ++i)
+ if ((i & 1) == 0)
+ EvenSum += i;
+}
+
+void findOdd(ull start, ull end) {
+ for (ull i = start; i <= end; ++i)
+ if ((i & 1) == 1)
+ OddSum += i;
+}
+
+int main() {
+
+ ull start = 0, end = 1900000000;
+
+ auto startTime = high_resolution_clock::now();
+ findOdd(start, end);
+ findEven(start, end);
+ auto stopTime = high_resolution_clock::now();
+ auto duration = duration_cast(stopTime - startTime);
+
+ cout << "OddSum : " << OddSum << endl;
+ cout << "EvenSum: " << EvenSum << endl;
+ cout << "Sec: " << duration.count() / 1000000 << endl;
+ return 0;
+}
diff --git a/Threading_In_CPlusPlus/1.thread/thread.cpp b/Threading_In_CPlusPlus/1.thread/thread.cpp
new file mode 100644
index 0000000..ae3ff93
--- /dev/null
+++ b/Threading_In_CPlusPlus/1.thread/thread.cpp
@@ -0,0 +1,56 @@
+//
+// Created by light on 20-1-31.
+//
+
+#include
+#include
+#include
+
+using namespace std::chrono;
+using namespace std;
+
+
+/**
+ * 1.普通函数指针
+ * 2.Lambda函数
+ * 3.Functors
+ * 4.非静态成员函数
+ * 5.静态成员函数
+ */
+
+using ull = unsigned long long;
+ull OddSum = 0;
+ull EvenSum = 0;
+
+void findEven(ull start, ull end) {
+ for (ull i = start; i <= end; ++i)
+ if ((i & 1) == 0)
+ EvenSum += i;
+}
+
+void findOdd(ull start, ull end) {
+ for (ull i = start; i <= end; ++i)
+ if ((i & 1) == 1)
+ OddSum += i;
+}
+
+int main() {
+
+ ull start = 0, end = 1900000000;
+
+
+ auto startTime = high_resolution_clock::now();
+ std::thread t1(findEven,start,end);
+ std::thread t2(findOdd,start,end);
+
+ t1.join();
+ t2.join();
+
+ auto stopTime = high_resolution_clock::now();
+ auto duration = duration_cast(stopTime - startTime);
+
+ cout << "OddSum : " << OddSum << endl;
+ cout << "EvenSum: " << EvenSum << endl;
+ cout << "Sec: " << duration.count() / 1000000 << endl;
+ return 0;
+}
diff --git a/Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp b/Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp
new file mode 100644
index 0000000..019a637
--- /dev/null
+++ b/Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp
@@ -0,0 +1,23 @@
+//
+// Created by light on 20-1-31.
+//
+
+// 1.函数指针
+#include
+#include
+
+using namespace std;
+
+void fun(int x) {
+ while (x-- > 0) {
+ cout << x << endl;
+ }
+}
+// 注意:如果我们创建多线程 并不会保证哪一个先开始
+int main() {
+ std::thread t1(fun, 10);
+// std::1.thread t2(fun, 10);
+ t1.join();
+// t2.join();
+ return 0;
+}
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp b/Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp
new file mode 100644
index 0000000..164e51e
--- /dev/null
+++ b/Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp
@@ -0,0 +1,31 @@
+//
+// Created by light on 20-1-31.
+//
+
+// 1.函数指针
+#include
+#include
+
+using namespace std;
+
+// 注意:如果我们创建多线程 并不会保证哪一个先开始
+int main() {
+ // 2.Lambda函数
+ auto fun = [](int x) {
+ while (x-- > 0) {
+ cout << x << endl;
+ }
+ };
+// std::1.thread t1(fun, 10);
+ // 也可以写成下面:
+ std::thread t1_1([](int x) {
+ while (x-- > 0) {
+ cout << x << endl;
+ }
+ }, 11);
+// std::1.thread t2(fun, 10);
+// t1.join();
+ t1_1.join();
+// t2.join();
+ return 0;
+}
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/2.create_type/3.functor.cpp b/Threading_In_CPlusPlus/2.create_type/3.functor.cpp
new file mode 100644
index 0000000..1787d27
--- /dev/null
+++ b/Threading_In_CPlusPlus/2.create_type/3.functor.cpp
@@ -0,0 +1,25 @@
+//
+// Created by light on 20-1-31.
+//
+
+
+#include
+#include
+
+using namespace std;
+
+// 3.functor (Funciton Object)
+class Base {
+public:
+ void operator()(int x) {
+ while (x-- > 0) {
+ cout << x << endl;
+ }
+ }
+};
+
+int main() {
+ thread t(Base(), 10);
+ t.join();
+ return 0;
+}
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp b/Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp
new file mode 100644
index 0000000..a20784b
--- /dev/null
+++ b/Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp
@@ -0,0 +1,26 @@
+//
+// Created by light on 20-1-31.
+//
+
+
+#include
+#include
+
+using namespace std;
+
+// 4.Non-static member function
+class Base {
+public:
+ void fun(int x) {
+ while (x-- > 0) {
+ cout << x << endl;
+ }
+ }
+};
+
+int main() {
+ Base b;
+ thread t(&Base::fun,&b, 10);
+ t.join();
+ return 0;
+}
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp b/Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp
new file mode 100644
index 0000000..39d5d1c
--- /dev/null
+++ b/Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp
@@ -0,0 +1,25 @@
+//
+// Created by light on 20-1-31.
+//
+
+
+#include
+#include
+
+using namespace std;
+
+// 4.Non-static member function
+class Base {
+public:
+ static void fun(int x) {
+ while (x-- > 0) {
+ cout << x << endl;
+ }
+ }
+};
+
+int main() {
+ thread t(&Base::fun, 10);
+ t.join();
+ return 0;
+}
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/3.join_detach/detach.cpp b/Threading_In_CPlusPlus/3.join_detach/detach.cpp
new file mode 100644
index 0000000..768f540
--- /dev/null
+++ b/Threading_In_CPlusPlus/3.join_detach/detach.cpp
@@ -0,0 +1,34 @@
+//
+// Created by light on 20-1-31.
+//
+
+// join 注意点
+
+/**
+ * 这用于从父线程分离新创建的线程
+ * 在分离线程之前,请务必检查它是否可以joinable,
+ * 否则可能会导致两次分离,并且双重detach()将导致程序终止
+ * 如果我们有分离的线程并且main函数正在返回,那么分离的线程执行将被挂起
+*/
+#include
+#include
+#include
+#include
+using namespace std;
+
+void run(int count) {
+ while (count-- > 0) {
+ cout << count << endl;
+ }
+ std::this_thread::sleep_for(chrono::seconds(3));
+}
+
+int main() {
+ thread t1(run, 10);
+ cout << "main()" << endl;
+ t1.detach();
+ if(t1.joinable())
+ t1.detach();
+ cout << "main() after" << endl;
+ return 0;
+}
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/3.join_detach/join.cpp b/Threading_In_CPlusPlus/3.join_detach/join.cpp
new file mode 100644
index 0000000..e2d9a6e
--- /dev/null
+++ b/Threading_In_CPlusPlus/3.join_detach/join.cpp
@@ -0,0 +1,35 @@
+//
+// Created by light on 20-1-31.
+//
+
+// join 注意点
+
+/**
+ * 一旦线程开始,我们要想等待线程完成,需要在该对象上调用join()
+ * 双重join将导致程序终止
+ * 在join之前我们应该检查显示是否可以被join,通过使用joinable()
+*/
+#include
+#include
+#include
+
+using namespace std;
+
+
+void run(int count) {
+ while (count-- > 0) {
+ cout << count << endl;
+ }
+ std::this_thread::sleep_for(chrono::seconds(3));
+}
+
+int main() {
+ thread t1(run, 10);
+ cout << "main()" << endl;
+ t1.join();
+ if (t1.joinable()) {
+ t1.join();
+ }
+ cout << "main() after" << endl;
+ return 0;
+}
\ No newline at end of file
diff --git a/Threading_In_CPlusPlus/4.mutex/critical_section.cpp b/Threading_In_CPlusPlus/4.mutex/critical_section.cpp
new file mode 100644
index 0000000..da30b44
--- /dev/null
+++ b/Threading_In_CPlusPlus/4.mutex/critical_section.cpp
@@ -0,0 +1,34 @@
+//
+// Created by light on 20-2-1.
+//
+#include
+#include
+#include
+
+using namespace std;
+
+int sum = 0; //shared
+
+mutex m;
+
+void *countgold() {
+ int i; //local to each thread
+ for (i = 0; i < 10000000; i++) {
+ m.lock();
+ sum += 1;
+ m.unlock();
+ }
+ return NULL;
+}
+
+int main() {
+ thread t1(countgold);
+ thread t2(countgold);
+
+ //Wait for both threads to finish
+ t1.join();
+ t2.join();
+
+ cout << "sum = " << sum << endl;
+ return 0;
+}
diff --git a/Threading_In_CPlusPlus/CMakeLists.txt b/Threading_In_CPlusPlus/CMakeLists.txt
new file mode 100644
index 0000000..84100a2
--- /dev/null
+++ b/Threading_In_CPlusPlus/CMakeLists.txt
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 3.14)
+project(Threading_In_CPlusPlus)
+
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
+
+add_executable(intro 1.thread/intro.cpp)
+add_executable(1.thread 1.thread/thread.cpp)
+add_executable(1.function_pointer 2.create_type/1.function_pointer.cpp)
+add_executable(2.lambda_function 2.create_type/2.lambda_function.cpp)
+add_executable(3.functor 2.create_type/3.functor.cpp)
+add_executable(4.static_member_function 2.create_type/4.no_static_member_function.cpp)
+add_executable(5.static_member_function 2.create_type/5.static_member_function.cpp)
+add_executable(join 3.join_detach/join.cpp)
+add_executable(detach 3.join_detach/detach.cpp)
+add_executable(critical_section 4.mutex/critical_section.cpp)
\ No newline at end of file