Skip to content

Commit da00f52

Browse files
changkunHarrisonDing
authored andcommitted
see changkun#2: update exercises and maintains of content
1 parent a049d09 commit da00f52

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+
//

exercises/6/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Makefile
33
# web_server
44
#
5-
# created by changkun at labex.io
5+
# created by changkun at changkun.de/modern-cpp
66
#
77

88
CXX = g++
@@ -15,15 +15,16 @@ SOURCE_HTTPS = main.https.cpp
1515
OBJECTS_HTTP = main.http.o
1616
OBJECTS_HTTPS = main.https.o
1717

18-
LDFLAGS_COMMON = -std=c++11 -O3 -pthread -lboost_system
18+
LDFLAGS_COMMON = -std=c++2a -O3 -pthread -lboost_system
1919
LDFLAGS_HTTP =
2020
LDFLAGS_HTTPS = -lssl -lcrypto
2121

2222
LPATH_COMMON = -I/usr/include/boost
2323
LPATH_HTTP =
24-
LPATH_HTTPS = -I/usr/include/openssl
24+
LPATH_HTTPS = -I/usr/local/opt/openssl/include
2525

2626
LLIB_COMMON = -L/usr/lib
27+
LLIB_HTTPS = -L/usr/local/opt/openssl/lib
2728

2829
all:
2930
make http

exercises/6/handler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <fstream>
99

1010
using namespace std;
11-
using namespace LabexWeb;
11+
using namespace Web;
1212

1313
template<typename SERVER_TYPE>
1414
void start_server(SERVER_TYPE &server) {

exercises/6/main.http.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "server.http.hpp"
99
#include "handler.hpp"
1010

11-
using namespace LabexWeb;
11+
using namespace Web;
1212

1313
int main() {
1414
// HTTP server runs in port 12345 HTTP, enable 4 threads

exercises/6/main.https.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <iostream>
77
#include "server.https.hpp"
88
#include "handler.hpp"
9-
using namespace LabexWeb;
9+
using namespace Web;
1010

1111
int main() {
1212
// HTTPS server runs in port 12345, enable 4 threads

exercises/6/server.base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <unordered_map>
1414
#include <thread>
1515

16-
namespace LabexWeb {
16+
namespace Web {
1717
struct Request {
1818
// request method, POST, GET; path; HTTP version
1919
std::string method, path, http_version;

exercises/6/server.http.hpp

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

1010
#include "server.base.hpp"
1111

12-
namespace LabexWeb {
12+
namespace Web {
1313
typedef boost::asio::ip::tcp::socket HTTP;
1414
template<>
1515
class Server<HTTP> : public ServerBase<HTTP> {

exercises/6/server.https.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "server.http.hpp"
1111
#include <boost/asio/ssl.hpp>
1212

13-
namespace LabexWeb {
13+
namespace Web {
1414

1515
// define HTTPS type
1616
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> HTTPS;

exercises/6/www/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<title>LabEx Web Server Test</title>
3+
<title>Web Server Test</title>
44
</head>
55
<body>
66
Hello world in index.html.

exercises/6/www/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<title>LabEx Web Server Test</title>
3+
<title>Web Server Test</title>
44
</head>
55
<body>
66
Hello world in test.html.

exercises/7/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Makefile
3+
#
4+
# exercise solution - chapter 7
5+
# modern cpp tutorial
6+
#
7+
# created by changkun at changkun.de/modern-cpp
8+
#
9+
10+
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))
11+
12+
%.out: %.cpp Makefile
13+
clang++ $< -o $@ -std=c++2a -pedantic
14+
15+
clean:
16+
rm *.out

exercises/7/main.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//
2+
// main.cpp
3+
//
4+
// exercise solution - chapter 7
5+
// modern cpp tutorial
6+
//
7+
// created by changkun at changkun.de/modern-cpp
8+
//
9+
110
#include <iostream> // std::cout, std::endl
211

312
#include <vector> // std::vector
@@ -6,7 +15,7 @@
615
#include <thread> // std::this_thread::sleep_for
716
#include <chrono> // std::chrono::seconds
817

9-
#include "ThreadPool.hpp"
18+
#include "thread_pool.hpp"
1019

1120
int main()
1221
{
@@ -35,6 +44,6 @@ int main()
3544
for(auto && result: results)
3645
std::cout << result.get() << ' ';
3746
std::cout << std::endl;
38-
47+
3948
return 0;
4049
}

0 commit comments

Comments
 (0)