File tree 1 file changed +2
-2
lines changed
1 file changed +2
-2
lines changed Original file line number Diff line number Diff line change @@ -294,7 +294,7 @@ C++ 标准库将这种事件称为 [future](https://zh.cppreference.com/w/cpp/t
294
294
295
295
C++ 标准库有两种 future,都声明在 [ ` <future> ` ] ( https://zh.cppreference.com/w/cpp/header/future ) 头文件中:独占的 [ ` std::future ` ] ( https://zh.cppreference.com/w/cpp/thread/future ) 、共享的 [ ` std::shared_future ` ] ( https://zh.cppreference.com/w/cpp/thread/shared_future ) 。它们的区别与 ` std::unique_ptr ` 和 ` std::shared_ptr ` 类似。` std::future ` 只能与** 单个** 指定事件关联,而 ` std::shared_future ` 能关联** 多个** 事件。它们都是模板,它们的模板类型参数,就是其关联的事件(函数)的返回类型。当多个线程需要访问一个独立 future 对象时, 必须使用互斥量或类似同步机制进行保护。而多个线程访问同一共享状态,若每个线程都是通过其自身的 ` shared_future ` 对象副本进行访问,则是安全的。
296
296
297
- 最简单的作用是 ,我们先前讲的 ` std::thread ` 执行任务是没有返回值的 ,这个问题就能使用 future 解决。
297
+ 最简单有效的使用是 ,我们先前讲的 ` std::thread ` 在线程中执行任务是没有返回值的 ,这个问题就能使用 future 解决。
298
298
299
299
### 创建异步任务获取返回值
300
300
@@ -1470,7 +1470,7 @@ int main(){
1470
1470
1471
1471
- 并发调用` barrier ` 除了析构函数外的成员函数不会引起数据竞争。
1472
1472
1473
- 另外你可能注意到我们使用了 [ ` std::osyncstream ` ] ( https://zh.cppreference.com/w/cpp/io/basic_osyncstream ) ,它是 C++20 引入的,此处是确保输出流在多线程环境中同步,** 免除数据竞争 ,而且将不以任何方式穿插或截断** 。
1473
+ 另外你可能注意到我们使用了 [ ` std::osyncstream ` ] ( https://zh.cppreference.com/w/cpp/io/basic_osyncstream ) ,它是 C++20 引入的,此处是确保输出流在多线程环境中同步,** 避免除数据竞争 ,而且将不以任何方式穿插或截断** 。
1474
1474
1475
1475
> 虽然 `std::cout` 的 `operator <<` 调用是线程安全的,不会被打断,但多个 `operator <<` 的调用在多线程环境中可能会**交错**,导致输出结果混乱,使用 `std::osyncstream` 就可以解决这个问题。开发者可以尝试去除 `std::osyncstream` 直接使用 `std::cout`,效果会非常明显。
1476
1476
You can’t perform that action at this time.
0 commit comments