Skip to content

Commit cc1d3d7

Browse files
committed
see #2: update exercises and maintains of content
1 parent fb91f15 commit cc1d3d7

24 files changed

+136
-69
lines changed

README-zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
## 随书习题
3333

34-
本书每章最后还加入了少量难度极小的习题,仅用于检验你是否能混合运用当前章节中的知识点。你可以在[这里](exercises)找到习题的答案,文件夹名称为章节序号。
34+
本书每章最后还加入了少量难度极小的习题,仅用于检验你是否能混合运用当前章节中的知识点。你可以在[这里](./exercises)找到习题的答案,文件夹名称为章节序号。
3535

3636
## 本书网站
3737

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Each chapter of this book has a lot of code. If you encounter problems when writ
3434

3535
## Exercises
3636

37-
There are few exercises At the end of each chapter of the book. It is for testing whether you can use the knowledge points in the current chapter. You can find the possible answer to the problem from [here](./exercise). The folder name is the chapter number.
37+
There are few exercises At the end of each chapter of the book. It is for testing whether you can use the knowledge points in the current chapter. You can find the possible answer to the problem from [here](./exercises). The folder name is the chapter number.
3838

3939
## Website
4040

book/en-us/toc.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@
7676
+ `std::regex`
7777
+ `std::regex_match`
7878
+ `std::match_results`
79-
- [**Chapter 07 Sandard Library: Threads and Concurrency**](./07-thread.md)
79+
- [**Chapter 07 Parallelism and Concurrency**](./07-thread.md)
8080
+ 7.1 `std::thread`
8181
+ 7.2 `std::mutex` and `std::unique_lock`
8282
+ 7.3 `std::future` and `std::packaged_task`
8383
+ 7.4 `std::condition_variable`
8484
+ 7.5 `std::atomic` and memory order
8585
+ 7.6 Transactional memory
86+
+ 7.7 Coroutine
8687
- [**Chapter 08 Sandard Library: File System**](./08-filesystem.md)
8788
+ 8.1 Documents and links
8889
+ 8.2 `std::filesystem`

book/zh-cn/06-regex.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ bar.txt sub-match[1]: bar
134134
135135
1. [知乎『如何评价 GCC 的 C++11 正则表达式?』中原库作者 Tim Shen 的回答](http://zhihu.com/question/23070203/answer/84248248)
136136
2. [正则表达式库文档](http://en.cppreference.com/w/cpp/regex)
137-
3. [C++ 开发 Web 服务框架](https://www.shiyanlou.com/courses/568)
138137
139138
## 许可
140139

book/zh-cn/07-thread.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
---
2-
title: 第 7 章 标准库:线程与并发
2+
title: 第 7 章 并行与并发
33
type: book-zh-cn
44
order: 7
55
---
66

7-
# 第 7 章 标准库:线程与并发
7+
# 第 7 章 并行与并发
88

99
> 内容修订中
1010
1111
[TOC]
1212

13-
## 7.1 std::thread
13+
## 7.1 线程与并行
14+
15+
### std::thread
1416

1517
`std::thread` 用于创建一个执行的线程实例,所以它是一切并发编程的基础,使用时需要包含 `<thread>` 头文件,它提供了很多基本的线程操作,例如`get_id()`来获取所创建线程的线程 ID,例如使用 `join()` 来加入一个线程等等,例如:
1618

@@ -179,16 +181,28 @@ consumer.join();
179181
180182
C++11 语言层提供了并发编程的相关支持,本节简单的介绍了 `std::thread`/`std::mutex`/`std::future` 这些并发编程中不可回避的重要工具。
181183
182-
> 本节提到的内容足以让我们使用不超过 100 行代码编写一个简单的线程池库,请参考习题 TODO
184+
## 习题
185+
186+
1. 请编写一个线程池,提供如下功能:
187+
188+
```cpp
189+
ThreadPool p(4); // 指定四个工作线程
190+
191+
// 将任务在池中入队,并返回一个 std::future
192+
auto f = pool.enqueue([](int life) {
193+
return meaning;
194+
}, 42);
195+
196+
// 从 future 中获得执行结果
197+
std::cout << f.get() << std::endl;
198+
```
183199

184200
[返回目录](./toc.md) | [上一章](./06-regex.md) | [下一章 标准库:文件系统](./08-filesystem.md)
185201

186202
## 进一步阅读的参考资料
187203

188204
1. [C++ 并发编程\(中文版\)](https://www.gitbook.com/book/chenxiaowei/cpp_concurrency_in_action/details)
189205
2. [线程支持库文档](http://en.cppreference.com/w/cpp/thread)
190-
3. [100 行 C++ 代码实现线程池](https://www.shiyanlou.com/teacher/courses/565)
191-
192206

193207
## 许可
194208

book/zh-cn/09-others.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,46 @@ void no_throw() noexcept; // 不可能抛出异常
3939
```cpp
4040
#include <iostream>
4141
void may_throw() {
42-
throw true;
42+
throw true;
4343
}
4444
auto non_block_throw = []{
45-
may_throw();
45+
may_throw();
4646
};
4747
void no_throw() noexcept {
48-
return;
48+
return;
4949
}
5050

5151
auto block_throw = []() noexcept {
52-
no_throw();
52+
no_throw();
5353
};
5454
int main()
5555
{
56-
std::cout << std::boolalpha
57-
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
58-
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
59-
<< "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
60-
<< "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
61-
return 0;
56+
std::cout << std::boolalpha
57+
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
58+
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
59+
<< "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
60+
<< "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
61+
return 0;
6262
}
6363
```
6464

6565
`noexcept` 修饰完一个函数之后能够起到封锁异常扩散的功效,如果内部产生异常,外部也不会触发。例如:
6666

6767
```cpp
6868
try {
69-
may_throw();
69+
may_throw();
7070
} catch (...) {
71-
std::cout << "捕获异常, 来自 my_throw()" << std::endl;
71+
std::cout << "捕获异常, 来自 my_throw()" << std::endl;
7272
}
7373
try {
74-
non_block_throw();
74+
non_block_throw();
7575
} catch (...) {
76-
std::cout << "捕获异常, 来自 non_block_throw()" << std::endl;
76+
std::cout << "捕获异常, 来自 non_block_throw()" << std::endl;
7777
}
7878
try {
79-
block_throw();
79+
block_throw();
8080
} catch (...) {
81-
std::cout << "捕获异常, 来自 block_throw()" << std::endl;
81+
std::cout << "捕获异常, 来自 block_throw()" << std::endl;
8282
}
8383
```
8484

@@ -116,19 +116,19 @@ C++11 引进了自定义字面量的能力,通过重载双引号后缀运算
116116

117117
// 字符串字面量自定义必须设置如下的参数列表
118118
std::string operator"" _wow1(const char *wow1, size_t len) {
119-
return std::string(wow1)+"woooooooooow, amazing";
119+
return std::string(wow1)+"woooooooooow, amazing";
120120
}
121121

122122
std::string operator"" _wow2 (unsigned long long i) {
123-
return std::to_string(i)+"woooooooooow, amazing";
123+
return std::to_string(i)+"woooooooooow, amazing";
124124
}
125125

126126
int main() {
127-
auto str = "abc"_wow1;
128-
auto num = 1_wow2;
129-
std::cout << str << std::endl;
130-
std::cout << num << std::endl;
131-
return 0;
127+
auto str = "abc"_wow1;
128+
auto num = 1_wow2;
129+
std::cout << str << std::endl;
130+
std::cout << num << std::endl;
131+
return 0;
132132
}
133133
```
134134

book/zh-cn/toc.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@
7676
+ `std::regex`
7777
+ `std::regex_match`
7878
+ `std::match_results`
79-
- [**第 7 章 标准库: 线程与并发**](./07-thread.md)
79+
- [**第 7 章 并行与并发**](./07-thread.md)
8080
+ 7.1 `std::thread`
8181
+ 7.2 `std::mutex``std::unique_lock`
8282
+ 7.3 `std::future``std::packaged_task`
8383
+ 7.4 `std::condition_variable`
8484
+ 7.5 `std::atomic` 与内存顺序
8585
+ 7.6 事务内存
86+
+ 7.7 协程
8687
- [**第 8 章 标准库: 文件系统**](./08-filesystem.md)
8788
+ 8.1 文档与链接
8889
+ 8.2 `std::filesystem`

exercises/2/fold.expresion.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//
2+
// fold.expression.cpp
3+
//
4+
// exercise solution - chapter 2
5+
// modern cpp tutorial
6+
//
7+
// created by changkun at changkun.de
8+
//
9+
110
#include <iostream>
211
template<typename ... T>
312
auto average(T ... t) {

exercises/2/structured.binding.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//
2+
// structured.binding.cpp
3+
//
4+
// exercise solution - chapter 2
5+
// modern cpp tutorial
6+
//
7+
// created by changkun at changkun.de
8+
//
9+
110
#include <iostream>
211
#include <map>
312
#include <string>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// variadic.template.parameter.pack.cpp
3+
//
4+
// exercise solution - chapter 2
5+
// modern cpp tutorial
6+
//
7+
// created by changkun at changkun.de
8+
//

0 commit comments

Comments
 (0)