C++笔记之std::async的用法

C++笔记之std::async的用法

code review!

文章目录

  • C++笔记之std::async的用法
    • 1.概念
    • [2.C++ 异步任务的使用示例 - 使用 std::async 和 std::future](#2.C++ 异步任务的使用示例 - 使用 std::async 和 std::future)
    • [3. std::launch::async 和 std::launch::deferred](#3. std::launch::async 和 std::launch::deferred)

1.概念

std::async 是 C++ 标准库中的一个函数,用于创建异步任务,允许在另一个线程中执行函数,并返回一个 std::future 对象,以便获取函数的结果。std::async 的一般用法如下:

cpp 复制代码
#include <iostream>
#include <future>

// 一个简单的函数,用于演示 std::async
int foo(int x, int y) {
    return x + y;
}

int main() {
    // 使用 std::async 创建异步任务
    std::future<int> result = std::async(foo, 3, 4);

    // 等待异步任务完成并获取结果
    int sum = result.get();

    std::cout << "Sum: " << sum << std::endl;
    
    return 0;
}

上述示例中,std::async 接受一个函数(foo)和它的参数,并在后台创建一个新线程来执行该函数。函数执行完毕后,可以使用 std::future 对象的 get() 方法来获取函数的返回值。

以下是 std::async 的一些重要注意事项和用法:

  1. 返回值类型:std::async 返回一个 std::future 对象,可以用于获取异步任务的返回值。

  2. 默认策略:std::async 可以接受一个可选的策略参数,例如 std::launch::asyncstd::launch::deferred,用于指定任务是立即在新线程中执行还是延迟执行。如果不提供策略,默认行为由实现决定。

  3. 异常处理:如果异步任务抛出异常,std::future 对象的 get() 方法会重新抛出该异常。因此,需要适当地处理异常,或者使用 std::futurewait()wait_for() 方法来检查任务是否出现异常。

  4. 返回值的共享和移动语义:根据策略,std::async 可能共享或移动参数,因此要小心在异步任务中使用共享数据。

  5. 后台线程管理:std::async 会自动创建和管理后台线程,但不提供对线程的直接控制。如果需要更多的线程控制功能,可以考虑使用 std::thread

  6. 取消任务:C++标准库在std::async中不提供直接的取消任务的机制,因此需要通过其他方式来实现任务的取消。

2.C++ 异步任务的使用示例 - 使用 std::async 和 std::future

运行

代码

cpp 复制代码
#include <iostream>
#include <future>

int main() {
    // 使用std::async创建一个异步任务,并返回一个std::future对象
    std::future<int> future_result = std::async(std::launch::async, []() {
        // 模拟一个耗时操作,返回一个结果
        std::this_thread::sleep_for(std::chrono::seconds(2));
        return 42;
    });

    std::cout << "Waiting for the future to become ready..." << std::endl;

    // 使用std::future::get()等待任务完成并获取结果
    int result = future_result.get();

    std::cout << "Async task result: " << result << std::endl;

    return 0;
}

3. std::launch::async 和 std::launch::deferred

std::launch::asyncstd::launch::deferred 是用于控制 std::async 函数的执行策略的标志,它们决定了异步任务的行为。这两个标志可以作为 std::launch 枚举类型的成员来使用。以下是它们的详细解释:

  1. std::launch::async

    • 当使用 std::launch::async 标志调用 std::async 时,函数将在新的线程中立即执行,即使没有调用 std::future 对象的 get() 方法也会执行。
    • 这意味着任务会异步执行,不会阻塞当前线程,而是在后台创建一个新线程来执行任务。
    • 如果你希望任务尽快执行,并且可以与其他操作并发执行,可以选择此策略。
  2. std::launch::deferred

    • 当使用 std::launch::deferred 标志调用 std::async 时,函数不会立即执行,而是等待调用 std::future 对象的 get() 方法时再执行。
    • 这意味着任务会延迟执行,直到你真正需要结果为止。如果未调用 get(),任务可能根本不会执行。
    • 这种策略适用于需要懒惰地计算结果的情况,以节省计算资源。

默认情况下,如果不指定策略,std::async 的行为由 C++ 标准库实现决定,可以是 std::launch::async 也可以是 std::launch::deferred,取决于具体实现。因此,为了明确指定策略,你可以使用以下方式:

cpp 复制代码
std::future<int> result = std::async(std::launch::async, foo, 3, 4); // 强制使用 std::launch::async

std::future<int> result = std::async(std::launch::deferred, foo, 3, 4); // 强制使用 std::launch::deferred

需要注意的是,使用 std::launch::async 策略可能会导致并发执行的线程数增加,而使用 std::launch::deferred 策略可能会导致任务延迟执行,因此需要根据具体情况选择适当的策略。

下面是一个完整的比较 std::launch::asyncstd::launch::deferred 策略的示例程序:

cpp 复制代码
#include <iostream>
#include <future>

int foo(int x, int y) {
    std::cout << "foo is running in thread " << std::this_thread::get_id() << std::endl;
    return x + y;
}

int main() {
    // 使用 std::launch::async 策略,任务会立即在新线程中执行
    std::cout << "Using std::launch::async:" << std::endl;
    std::future<int> async_result = std::async(std::launch::async, foo, 3, 4);

    // 使用 std::launch::deferred 策略,任务会延迟执行,直到调用 get() 方法
    std::cout << "Using std::launch::deferred:" << std::endl;
    std::future<int> deferred_result = std::async(std::launch::deferred, foo, 3, 4);

    // 主线程继续执行其他工作
    std::this_thread::sleep_for(std::chrono::seconds(2));
    
    // 获取 std::launch::async 的结果(可能已经执行完毕)
    int async_sum = async_result.get();
    std::cout << "Result from std::launch::async: " << async_sum << std::endl;

    // 获取 std::launch::deferred 的结果(现在才执行)
    int deferred_sum = deferred_result.get();
    std::cout << "Result from std::launch::deferred: " << deferred_sum << std::endl;
    
    return 0;
}

在这个示例中,我们首先使用 std::launch::asyncstd::launch::deferred 策略调用了 foo 函数。foo 函数会输出当前线程的标识符。然后,主线程休眠了2秒钟以模拟其他工作。

接着,我们分别调用 async_result.get()deferred_result.get() 来获取两种策略下的结果。你会注意到,std::launch::async 的任务会立即执行(可能已经执行完毕),而 std::launch::deferred 的任务直到调用 get() 时才会执行。

这个示例演示了 std::launch::asyncstd::launch::deferred 的不同行为,以及如何使用它们来控制异步任务的执行方式。

相关推荐
静心观复17 分钟前
drawio画java的uml的类图时,class和interface的区别是什么
java·uml·draw.io
moringlightyn17 分钟前
c++11可变模版参数 emplace接口 新的类功能 lambda 包装器
开发语言·c++·笔记·其他·c++11·lambda·包装器
Laplaces Demon19 分钟前
Spring 源码学习(十四)—— HandlerMethodArgumentResolver
java·开发语言·学习
崎岖Qiu21 分钟前
【OS笔记11】:进程和线程9-死锁及其概念
笔记·操作系统·os
郝学胜-神的一滴22 分钟前
使用Linux系统函数递归遍历指定目录
linux·运维·服务器·开发语言·c++·软件工程
guygg8822 分钟前
Java 无锁方式实现高性能线程
java·开发语言
ss27324 分钟前
手写Spring第7弹:Spring IoC容器深度解析:XML配置的完整指南
java·前端·数据库
青衫码上行1 小时前
【从0开始学习Java | 第22篇】反射
java·开发语言·学习
superlls1 小时前
(Spring)Spring Boot 中 @Valid 与全局异常处理器的联系详解
java·spring boot·后端
choice of1 小时前
Sentinel:阿里云高并发流量控制
笔记·spring cloud·sentinel