std::asyncstd::asyncstd::asyncstd::async

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

文章目录
C++笔记之std::async的用法
1.概念
2.C++ 异步任务的使用示例 - 使用 std::async 和 std::future
3. std::launch::async 和 std::launch::deferred
4.如果需要真正的异步,请指定std::launch::async
1.概念
std::async 是 C++ 标准库中的一个函数,用于创建异步任务,允许在另一个线程中执行函数,并返回一个 std::future 对象,以便获取函数的结果。std::async 的一般用法如下:


#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 的一些重要注意事项和用法:

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

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

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

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

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

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

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


运行(GIF动图)

代码

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

std::launch::async:

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

当使用 std::launch::deferred 标志调用 std::async 时,函数不会立即执行,而是等待调用 std::future 对象的 get() 方法时再执行。
这意味着任务会延迟执行,直到你真正需要结果为止。如果未调用 get(),任务可能根本不会执行。
这种策略适用于需要懒惰地计算结果的情况,以节省计算资源。
默认情况下,如果不指定策略,std::async 的行为由 C++ 标准库实现决定,可以是 std::launch::async 也可以是 std::launch::deferred,取决于具体实现。因此,为了明确指定策略,你可以使用以下方式:

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

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

#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::async 和 std::launch::deferred 策略调用了 foo 函数。foo 函数会输出当前线程的标识符。然后,主线程休眠了2秒钟以模拟其他工作。

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

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

4.如果需要真正的异步,请指定std::launch::async


文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树首页概览136899 人正在系统学习中
————————————————
版权声明:本文为CSDN博主「正在找女朋友的害羞男孩」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43297891/article/details/133829964

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值