mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
see #2: update exercises and maintains of content
This commit is contained in:
@@ -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)
|
||||
|
||||
## 许可
|
||||
|
||||
|
||||
@@ -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()` 来加入一个线程等等,例如:
|
||||
|
||||
@@ -179,7 +181,21 @@ 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)
|
||||
|
||||
@@ -187,8 +203,6 @@ C++11 语言层提供了并发编程的相关支持,本节简单的介绍了 `
|
||||
|
||||
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)
|
||||
|
||||
|
||||
## 许可
|
||||
|
||||
|
||||
@@ -39,26 +39,26 @@ 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;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -66,19 +66,19 @@ return 0;
|
||||
|
||||
```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;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -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`
|
||||
|
||||
Reference in New Issue
Block a user