Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc22a15

Browse files
committed
修改 std::async 的部分描述
1 parent 07fa631 commit fc22a15

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

‎md/04同步操作.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,12 @@ int main(){
373373

374374
---
375375

376-
接下来我们聊 `std::async` 的执行策略,我们前面一直没有使用,其实就是在传递可调用对象与参数之前传递枚举值罢了:
376+
接下来我们聊 `std::async` [执行策略](https://zh.cppreference.com/w/cpp/thread/launch),我们前面一直没有使用,其实就是在传递可调用对象与参数之前传递枚举值罢了:
377377

378378
1. `std::launch::async` 在不同**线程上**执行异步任务。
379379
2. `std::launch::deferred` 惰性求值,**不创建线程**,等待 `future` 对象调用 `wait``get` 成员函数的时候执行任务。
380380

381-
而我们先前没有写明这个参数,实际上是**默认**:`std::launch::async | std::launch::deferred` ,也就是说由实现选择到底是否创建线程执行异步任务。典型情况是,如果系统资源充足,并且异步任务的执行不会导致性能问题,那么系统可能会选择在新线程中执行任务。但是,如果系统资源有限,或者延迟执行可以提高性能或节省资源,那么系统可能会选择延迟执行。
381+
而我们先前一直没有写明这个参数,是因为 `std::async` 函数模板有两个**重载**,不给出执行策略就是以:`std::launch::async | std::launch::deferred` 调用另一个重载版本(这一点中在[源码](https://github.com/microsoft/STL/blob/f54203f/stl/inc/future#L1425-L1430)中很明显),此策略表示由实现选择到底是否创建线程执行异步任务。典型情况是,如果系统资源充足,并且异步任务的执行不会导致性能问题,那么系统可能会选择在新线程中执行任务。但是,如果系统资源有限,或者延迟执行可以提高性能或节省资源,那么系统可能会选择延迟执行。
382382

383383
我们来展示一下:
384384

@@ -402,7 +402,7 @@ int main(){
402402

403403
其实到此基本就差不多了,我们再介绍两个常见问题即可:
404404

405-
1. 如果从 `std::async` 获得的 [std::future](https://zh.cppreference.com/w/cpp/thread/future) 没有被移动或绑定到引用,那么在完整表达式结尾, [std::future](https://zh.cppreference.com/w/cpp/thread/future)**析构函数将阻塞到异步计算完成**
405+
1. 如果从 `std::async` 获得的 [std::future](https://zh.cppreference.com/w/cpp/thread/future) 没有被移动或绑定到引用,那么在完整表达式结尾, [std::future](https://zh.cppreference.com/w/cpp/thread/future)**析构函数将阻塞到异步计算完成**因为临时对象的生存期就在这一行,调用析构函数阻塞执行。
406406

407407
```cpp
408408
std::async(std::launch::async, []{ f(); }); // 临时量的析构函数等待 f()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /