doc: update cpp.md #255
This commit is contained in:
parent
bb533b9081
commit
f40b56ebb8
109
docs/cpp.md
109
docs/cpp.md
@ -633,19 +633,20 @@ std::for_each(vec.begin(), vec.end(),
|
|||||||
|
|
||||||
## C++多线程
|
## C++多线程
|
||||||
|
|
||||||
> g++编译选项:`-std=c++11`
|
### 多线程介绍
|
||||||
>
|
|
||||||
> 包含头文件:
|
g++编译选项:`-std=c++11`。包含头文件:
|
||||||
>
|
|
||||||
> + `#include <thread>`:C++多线程库
|
- `#include <thread>`:C++多线程库
|
||||||
> + `#include <mutex>`:C++互斥量库
|
- `#include <mutex>`:C++互斥量库
|
||||||
> + `#include <future>`:C++异步库
|
- `#include <future>`:C++异步库
|
||||||
|
|
||||||
### 线程的创建
|
### 线程的创建
|
||||||
|
<!--rehype:wrap-class=row-span-2-->
|
||||||
|
|
||||||
以普通函数作为线程入口函数:
|
以普通函数作为线程入口函数:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
void entry_1() { }
|
void entry_1() { }
|
||||||
void entry_2(int val) { }
|
void entry_2(int val) { }
|
||||||
|
|
||||||
@ -655,7 +656,7 @@ std::thread my_thread_2(entry_2, 5);
|
|||||||
|
|
||||||
以类对象作为线程入口函数:
|
以类对象作为线程入口函数:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
class Entry
|
class Entry
|
||||||
{
|
{
|
||||||
void operator()() { }
|
void operator()() { }
|
||||||
@ -671,7 +672,7 @@ std::thread my_thread_2(&Entry::entry_function, &entry);
|
|||||||
|
|
||||||
以lambda表达式作为线程入口函数:
|
以lambda表达式作为线程入口函数:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::thread my_thread([]() -> void
|
std::thread my_thread([]() -> void
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@ -680,7 +681,7 @@ std::thread my_thread([]() -> void
|
|||||||
|
|
||||||
### 线程的销毁
|
### 线程的销毁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
thread my_thread;
|
thread my_thread;
|
||||||
// 阻塞
|
// 阻塞
|
||||||
my_thread.join();
|
my_thread.join();
|
||||||
@ -690,7 +691,7 @@ my_thread.detach();
|
|||||||
|
|
||||||
### `this_thread`
|
### `this_thread`
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
// 获取当前线程ID
|
// 获取当前线程ID
|
||||||
std::this_thread::get_id();
|
std::this_thread::get_id();
|
||||||
// 使当前线程休眠一段指定时间
|
// 使当前线程休眠一段指定时间
|
||||||
@ -702,6 +703,7 @@ std::this_thread::yield();
|
|||||||
```
|
```
|
||||||
|
|
||||||
### 锁
|
### 锁
|
||||||
|
<!--rehype:wrap-class=row-span-3-->
|
||||||
|
|
||||||
> `#include <mutex>`
|
> `#include <mutex>`
|
||||||
|
|
||||||
@ -709,46 +711,46 @@ std::this_thread::yield();
|
|||||||
|
|
||||||
创建锁
|
创建锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::mutex m;
|
std::mutex m;
|
||||||
```
|
```
|
||||||
|
|
||||||
上锁
|
上锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
m.lock();
|
m.lock();
|
||||||
```
|
```
|
||||||
|
|
||||||
解锁
|
解锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
m.unlock();
|
m.unlock();
|
||||||
```
|
```
|
||||||
|
|
||||||
尝试上锁:成功返回`true`,失败返回`false`
|
尝试上锁:成功返回`true`,失败返回`false`
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
m.try_lock();
|
m.try_lock();
|
||||||
```
|
```
|
||||||
|
|
||||||
解锁
|
解锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
m.unlock();
|
m.unlock();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 更简单的锁——`std::lock_guard<Mutex>`
|
#### 更简单的锁 —— `std::lock_guard<Mutex>`
|
||||||
|
|
||||||
构造时上锁,析构时解锁
|
构造时上锁,析构时解锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::mutex m;
|
std::mutex m;
|
||||||
std::lock_guard<std::mutex> lock(m);
|
std::lock_guard<std::mutex> lock(m);
|
||||||
```
|
```
|
||||||
|
|
||||||
额外参数:`std::adopt_lock`:只需解锁,无需上锁
|
额外参数:`std::adopt_lock`:只需解锁,无需上锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
// 手动上锁
|
// 手动上锁
|
||||||
m.lock();
|
m.lock();
|
||||||
std::lock_guard<mutex> lock(m,
|
std::lock_guard<mutex> lock(m,
|
||||||
@ -759,7 +761,7 @@ std::lock_guard<mutex> lock(m,
|
|||||||
|
|
||||||
构造上锁,析构解锁
|
构造上锁,析构解锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::mutex m;
|
std::mutex m;
|
||||||
std::unique_lock<mutex> lock(m);
|
std::unique_lock<mutex> lock(m);
|
||||||
```
|
```
|
||||||
@ -768,7 +770,7 @@ std::unique_lock<mutex> lock(m);
|
|||||||
|
|
||||||
只需解锁,无需上锁
|
只需解锁,无需上锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
// 手动上锁
|
// 手动上锁
|
||||||
m.lock();
|
m.lock();
|
||||||
std::unique_lock<mutex> lock(m,
|
std::unique_lock<mutex> lock(m,
|
||||||
@ -779,7 +781,7 @@ std::unique_lock<mutex> lock(m,
|
|||||||
|
|
||||||
尝试上锁,可以通过`std::unique_lock<Mutex>::owns_lock()`查看状态
|
尝试上锁,可以通过`std::unique_lock<Mutex>::owns_lock()`查看状态
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::unique_lock<mutex> lock(m,
|
std::unique_lock<mutex> lock(m,
|
||||||
std::try_to_lock);
|
std::try_to_lock);
|
||||||
if (lock.owns_lock())
|
if (lock.owns_lock())
|
||||||
@ -796,7 +798,7 @@ else
|
|||||||
|
|
||||||
绑定锁,但不上锁
|
绑定锁,但不上锁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::unique_lock<mutex> lock(m,
|
std::unique_lock<mutex> lock(m,
|
||||||
std::defer_lock);
|
std::defer_lock);
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@ -811,7 +813,7 @@ lock.unlock();
|
|||||||
|
|
||||||
当多个线程通过这个函数调用一个可调用对象时,只会有一个线程成功调用。
|
当多个线程通过这个函数调用一个可调用对象时,只会有一个线程成功调用。
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::once_flag flag;
|
std::once_flag flag;
|
||||||
|
|
||||||
void foo() { }
|
void foo() { }
|
||||||
@ -823,13 +825,13 @@ std::call_once(flag, foo);
|
|||||||
|
|
||||||
#### 创建条件变量
|
#### 创建条件变量
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::condition_variable cond;
|
std::condition_variable cond;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 等待条件变量被通知
|
#### 等待条件变量被通知
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::unique_lock<std::mutex>
|
std::unique_lock<std::mutex>
|
||||||
lock;
|
lock;
|
||||||
extern bool predicate();
|
extern bool predicate();
|
||||||
@ -840,28 +842,31 @@ cond.wait(lock);
|
|||||||
cond.wait(lock, predicate);
|
cond.wait(lock, predicate);
|
||||||
```
|
```
|
||||||
|
|
||||||
1. `wait`不断地尝试重新获取并加锁该互斥量,如果获取不到,它就卡在这里并反复尝试重新获取,如果获取到了,执行流程就继续往下走
|
----
|
||||||
2. `wait`在获取到互斥量并加锁了互斥量之后:
|
|
||||||
1. 如果`wait`被提供了可调用对象,那么就执行这个可调用对象:
|
- `wait`不断地尝试重新获取并加锁该互斥量,如果获取不到,它就卡在这里并反复尝试重新获取,如果获取到了,执行流程就继续往下走
|
||||||
+ 如果返回值为`false`,那么`wait`继续加锁,直到再次被notified
|
- `wait`在获取到互斥量并加锁了互斥量之后:
|
||||||
+ 如果返回值为`true`,那么`wait`返回,继续执行流程
|
- 如果`wait`被提供了可调用对象,那么就执行这个可调用对象:
|
||||||
2. 如果`wait`没有第二个参数,那么直接返回,继续执行
|
- 如果返回值为`false`,那么`wait`继续加锁,直到再次被 notified
|
||||||
|
- 如果返回值为`true`,那么`wait`返回,继续执行流程
|
||||||
|
- 如果`wait`没有第二个参数,那么直接返回,继续执行
|
||||||
|
|
||||||
#### `std::condition_variable::notify_one`
|
#### `std::condition_variable::notify_one`
|
||||||
|
|
||||||
`notify_one`唤醒一个调用`wait`的线程。注意在唤醒之前要解锁,否则调用`wait`的线程也会因为无法加锁而阻塞。
|
`notify_one` 唤醒一个调用 `wait` 的线程。注意在唤醒之前要解锁,否则调用 `wait` 的线程也会因为无法加锁而阻塞。
|
||||||
|
|
||||||
#### `std::condition_variable::notify_all`
|
#### `std::condition_variable::notify_all`
|
||||||
|
|
||||||
唤醒所有调用`wait`的线程。
|
唤醒所有调用 `wait` 的线程。
|
||||||
|
|
||||||
### 获取线程的运行结果
|
### 获取线程的运行结果
|
||||||
|
<!--rehype:wrap-class=row-span-2-->
|
||||||
|
|
||||||
> `#include <future>`
|
> `#include <future>`
|
||||||
|
|
||||||
#### 创建异步任务
|
#### 创建异步任务
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
double func(int val);
|
double func(int val);
|
||||||
|
|
||||||
// 使用std::async创建异步任务
|
// 使用std::async创建异步任务
|
||||||
@ -875,34 +880,34 @@ std::future<double> result =
|
|||||||
|
|
||||||
等待异步任务结束,但是不获取返回值:
|
等待异步任务结束,但是不获取返回值:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
result.wait();
|
result.wait();
|
||||||
```
|
```
|
||||||
|
|
||||||
获取异步任务的返回值:
|
获取异步任务的返回值:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
int val = result.get();
|
int val = result.get();
|
||||||
```
|
```
|
||||||
|
|
||||||
注:
|
注:
|
||||||
|
|
||||||
+ `get()`返回右值,因此只可调用一次
|
- `get()`返回右值,因此只可调用一次
|
||||||
+ 只要调用上述任意函数,线程就会一直阻塞到返回值可用(入口函数运行结束)
|
- 只要调用上述任意函数,线程就会一直阻塞到返回值可用(入口函数运行结束)
|
||||||
|
|
||||||
#### `std::async`的额外参数
|
#### `std::async` 的额外参数
|
||||||
|
|
||||||
额外参数可以被放在`std::async`的第一个参数位置,用于设定`std::async`的行为:
|
额外参数可以被放在 `std::async` 的第一个参数位置,用于设定 `std::async` 的行为:
|
||||||
|
|
||||||
1. `std::launch::deferred`:入口函数的运行会被推迟到`std::future<T>::get()`或者`std::future<T>::wait()`被调用时。此时调用线程会直接运行线程入口函数,换言之,**不会创建子线程**
|
- `std::launch::deferred`:入口函数的运行会被推迟到`std::future<T>::get()`或者`std::future<T>::wait()`被调用时。此时调用线程会直接运行线程入口函数,换言之,**不会创建子线程**
|
||||||
2. `std::launch::async`:立即创建子线程,并运行线程入口函数
|
- `std::launch::async`:立即创建子线程,并运行线程入口函数
|
||||||
3. `std::launch::deferred | std::launch::async`:默认值,由系统自行决定
|
- `std::launch::deferred | std::launch::async`:默认值,由系统自行决定
|
||||||
|
|
||||||
#### 返回值的状态
|
#### 返回值的状态
|
||||||
|
|
||||||
让当前线程等待一段时间(等待到指定时间点),以期待返回值准备好:
|
让当前线程等待一段时间(等待到指定时间点),以期待返回值准备好:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
extern double foo(int val) {}
|
extern double foo(int val) {}
|
||||||
|
|
||||||
std::future<double> result =
|
std::future<double> result =
|
||||||
@ -923,7 +928,7 @@ status = result.wait_for(
|
|||||||
|
|
||||||
在指定的时间过去后,可以获取等待的结果:
|
在指定的时间过去后,可以获取等待的结果:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
// 返回值已经准备好
|
// 返回值已经准备好
|
||||||
if (status ==
|
if (status ==
|
||||||
std::future_status::ready)
|
std::future_status::ready)
|
||||||
@ -933,20 +938,16 @@ if (status ==
|
|||||||
// 超时:尚未准备好
|
// 超时:尚未准备好
|
||||||
else if (status ==
|
else if (status ==
|
||||||
std::future_status::timeout)
|
std::future_status::timeout)
|
||||||
{
|
{ }
|
||||||
|
|
||||||
}
|
|
||||||
// 尚未启动: std::launch::deferred
|
// 尚未启动: std::launch::deferred
|
||||||
else if (status ==
|
else if (status ==
|
||||||
std::future_status::deferred)
|
std::future_status::deferred)
|
||||||
{
|
{ }
|
||||||
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 多个返回值
|
#### 多个返回值
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::shared_future<T> result;
|
std::shared_future<T> result;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user