Skip to content

Commit

Permalink
see changkun#2: update exercises and maintains of content
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Jul 10, 2019
1 parent fb91f15 commit cc1d3d7
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README-zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

## 随书习题

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

## 本书网站

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Each chapter of this book has a lot of code. If you encounter problems when writ

## Exercises

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.
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.

## Website

Expand Down
3 changes: 2 additions & 1 deletion book/en-us/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@
+ `std::regex`
+ `std::regex_match`
+ `std::match_results`
- [**Chapter 07 Sandard Library: Threads and Concurrency**](./07-thread.md)
- [**Chapter 07 Parallelism and Concurrency**](./07-thread.md)
+ 7.1 `std::thread`
+ 7.2 `std::mutex` and `std::unique_lock`
+ 7.3 `std::future` and `std::packaged_task`
+ 7.4 `std::condition_variable`
+ 7.5 `std::atomic` and memory order
+ 7.6 Transactional memory
+ 7.7 Coroutine
- [**Chapter 08 Sandard Library: File System**](./08-filesystem.md)
+ 8.1 Documents and links
+ 8.2 `std::filesystem`
Expand Down
1 change: 0 additions & 1 deletion book/zh-cn/06-regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ bar.txt sub-match[1]: bar
1. [知乎『如何评价 GCC 的 C++11 正则表达式?』中原库作者 Tim Shen 的回答](http://zhihu.com/question/23070203/answer/84248248)
2. [正则表达式库文档](http://en.cppreference.com/w/cpp/regex)
3. [C++ 开发 Web 服务框架](https://www.shiyanlou.com/courses/568)
## 许可
Expand Down
26 changes: 20 additions & 6 deletions book/zh-cn/07-thread.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
title: 第 7 章 标准库:线程与并发
title: 第 7 章 并行与并发
type: book-zh-cn
order: 7
---

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

> 内容修订中
[TOC]

## 7.1 std::thread
## 7.1 线程与并行

### std::thread

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

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

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

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

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

## 许可

Expand Down
46 changes: 23 additions & 23 deletions book/zh-cn/09-others.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,46 @@ void no_throw() noexcept; // 不可能抛出异常
```cpp
#include <iostream>
void may_throw() {
throw true;
throw true;
}
auto non_block_throw = []{
may_throw();
may_throw();
};
void no_throw() noexcept {
return;
return;
}

auto block_throw = []() noexcept {
no_throw();
no_throw();
};
int main()
{
std::cout << std::boolalpha
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
<< "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
<< "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
return 0;
std::cout << std::boolalpha
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
<< "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
<< "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
return 0;
}
```

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

```cpp
try {
may_throw();
may_throw();
} catch (...) {
std::cout << "捕获异常, 来自 my_throw()" << std::endl;
std::cout << "捕获异常, 来自 my_throw()" << std::endl;
}
try {
non_block_throw();
non_block_throw();
} catch (...) {
std::cout << "捕获异常, 来自 non_block_throw()" << std::endl;
std::cout << "捕获异常, 来自 non_block_throw()" << std::endl;
}
try {
block_throw();
block_throw();
} catch (...) {
std::cout << "捕获异常, 来自 block_throw()" << std::endl;
std::cout << "捕获异常, 来自 block_throw()" << std::endl;
}
```

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

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

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

int main() {
auto str = "abc"_wow1;
auto num = 1_wow2;
std::cout << str << std::endl;
std::cout << num << std::endl;
return 0;
auto str = "abc"_wow1;
auto num = 1_wow2;
std::cout << str << std::endl;
std::cout << num << std::endl;
return 0;
}
```

Expand Down
3 changes: 2 additions & 1 deletion book/zh-cn/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@
+ `std::regex`
+ `std::regex_match`
+ `std::match_results`
- [**第 7 章 标准库: 线程与并发**](./07-thread.md)
- [**第 7 章 并行与并发**](./07-thread.md)
+ 7.1 `std::thread`
+ 7.2 `std::mutex``std::unique_lock`
+ 7.3 `std::future``std::packaged_task`
+ 7.4 `std::condition_variable`
+ 7.5 `std::atomic` 与内存顺序
+ 7.6 事务内存
+ 7.7 协程
- [**第 8 章 标准库: 文件系统**](./08-filesystem.md)
+ 8.1 文档与链接
+ 8.2 `std::filesystem`
Expand Down
9 changes: 9 additions & 0 deletions exercises/2/fold.expresion.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
//
// fold.expression.cpp
//
// exercise solution - chapter 2
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>
template<typename ... T>
auto average(T ... t) {
Expand Down
9 changes: 9 additions & 0 deletions exercises/2/structured.binding.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
//
// structured.binding.cpp
//
// exercise solution - chapter 2
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>
#include <map>
#include <string>
Expand Down
8 changes: 8 additions & 0 deletions exercises/2/variadic.template.parameter.pack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// variadic.template.parameter.pack.cpp
//
// exercise solution - chapter 2
// modern cpp tutorial
//
// created by changkun at changkun.de
//
7 changes: 4 additions & 3 deletions exercises/6/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Makefile
# web_server
#
# created by changkun at labex.io
# created by changkun at changkun.de/modern-cpp
#

CXX = g++
Expand All @@ -15,15 +15,16 @@ SOURCE_HTTPS = main.https.cpp
OBJECTS_HTTP = main.http.o
OBJECTS_HTTPS = main.https.o

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

LPATH_COMMON = -I/usr/include/boost
LPATH_HTTP =
LPATH_HTTPS = -I/usr/include/openssl
LPATH_HTTPS = -I/usr/local/opt/openssl/include

LLIB_COMMON = -L/usr/lib
LLIB_HTTPS = -L/usr/local/opt/openssl/lib

all:
make http
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <fstream>

using namespace std;
using namespace LabexWeb;
using namespace Web;

template<typename SERVER_TYPE>
void start_server(SERVER_TYPE &server) {
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/main.http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "server.http.hpp"
#include "handler.hpp"

using namespace LabexWeb;
using namespace Web;

int main() {
// HTTP server runs in port 12345 HTTP, enable 4 threads
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/main.https.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <iostream>
#include "server.https.hpp"
#include "handler.hpp"
using namespace LabexWeb;
using namespace Web;

int main() {
// HTTPS server runs in port 12345, enable 4 threads
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/server.base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <unordered_map>
#include <thread>

namespace LabexWeb {
namespace Web {
struct Request {
// request method, POST, GET; path; HTTP version
std::string method, path, http_version;
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/server.http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "server.base.hpp"

namespace LabexWeb {
namespace Web {
typedef boost::asio::ip::tcp::socket HTTP;
template<>
class Server<HTTP> : public ServerBase<HTTP> {
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/server.https.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "server.http.hpp"
#include <boost/asio/ssl.hpp>

namespace LabexWeb {
namespace Web {

// define HTTPS type
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> HTTPS;
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/www/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
<title>LabEx Web Server Test</title>
<title>Web Server Test</title>
</head>
<body>
Hello world in index.html.
Expand Down
2 changes: 1 addition & 1 deletion exercises/6/www/test.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
<title>LabEx Web Server Test</title>
<title>Web Server Test</title>
</head>
<body>
Hello world in test.html.
Expand Down
16 changes: 16 additions & 0 deletions exercises/7/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Makefile
#
# exercise solution - chapter 7
# modern cpp tutorial
#
# created by changkun at changkun.de/modern-cpp
#

all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))

%.out: %.cpp Makefile
clang++ $< -o $@ -std=c++2a -pedantic

clean:
rm *.out
13 changes: 11 additions & 2 deletions exercises/7/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
//
// main.cpp
//
// exercise solution - chapter 7
// modern cpp tutorial
//
// created by changkun at changkun.de/modern-cpp
//

#include <iostream> // std::cout, std::endl

#include <vector> // std::vector
Expand All @@ -6,7 +15,7 @@
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds

#include "ThreadPool.hpp"
#include "thread_pool.hpp"

int main()
{
Expand Down Expand Up @@ -35,6 +44,6 @@ int main()
for(auto && result: results)
std::cout << result.get() << ' ';
std::cout << std::endl;

return 0;
}
Loading

0 comments on commit cc1d3d7

Please sign in to comment.