c++ async 使用详解,创建异步任务的多种方法

c++ async 使用详解

std::async

  • 头文件 #include <future>

  • 函数原型:

    template<class Function, class... Args>
    std::future<std::invoke_result_t<std::decay_t<Function>,
                                     std::decay_t<Args>...>>
        async(std::launch policy, Function&& f, Args&&... args);
    
    template<class Function, class... Args>
    std::future<std::invoke_result_t<std::decay_t<Function>,
                                     std::decay_t<Args>...>>
        async(Function&& f, Args&&... args);
    // 等同于以 std::launch::async | std::launch::deferred 策略调用上面的函数
    
  • 函数参数:

    • policy:以何种策略调用可调用对象 f,可以为以下三种:
      • std::launch::async:以异步的方式调用 f,即必须另外开启专属的线程,在其上运行 f。
      • std::launch::deferred:在返回的 std::future 上调用了非定时等待函数,即 wait 或者 get 时,才执行 f。
      • std::launch::async | std::launch::deferred:可能异步运行 f 或者直到调用 wait 或者 get 时才运行,取决于系统的负载,无法人为控制。
    • f:要调用的可调用对象。
    • args:传递给 f 的参数。
  • 函数返回值:

    • 返回 std::future 对象,在其上调用 wait 可以等待 f 完成,调用 get 可以等待并获取 f 的返回值。 -
  • 函数作用:

    • 以异步或者同步的调用可调用对象 f,并可以通过返回的 std::future 对象获取 f 的返回值。

使用注意

  • 如果没有任何对象接收 std::sync 的返回值,即使指定了 std::launch::async 策略,std::future 的析构函数也会阻塞直到整个调用完成。

  • 示例如下:

    // 临时量即 std::future 的析构函数等待睡眠完成。
    std::async(std::launch::async, []{ std::this_thread::sleep_for(10ms); });
    // 在睡眠完成之前,本行代码不会得到运行
    std::async(std::launch::async, []{ printf("xx"); });
    
  • 只有从 std::async 获取的 std::future 对象其析构函数会阻塞,以其他方式获得的 std::future 对象则不会。

示例1 函数作为参数

#include <stdio.h>
#include <thread>
#include <future>
#include <chrono>

int f1(int n, int& b)
{
    for (int i = 0; i < 3; ++i) {
        printf("async is running\n");
        ++n;
        ++b;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    printf("n is %d, b is %d\n", n, b);
    return n + b;
}

int main()
{
    int n = 0;
    int b = 0;
    // n 按值传递,b 按引用传递
    auto f = std::async(std::launch::async, f1, n, std::ref(b));
    int ret = f.get();
    printf("result is %d\n", ret);
    return 0;
}

示例2 lamba 作为参数

#include <stdio.h>
#include <thread>
#include <future>
#include <chrono>

int main()
{
    auto f = std::async([]() -> int {
        for (int i = 0; i < 3; ++i) {
            printf("async is running\n");
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
        return 100;
    });
    int ret = f.get();
    printf("result is %d\n", ret);
    return 0;
}

示例3 std::function 作为参数

#include <stdio.h>
#include <thread>
#include <future>
#include <chrono>
#include <functional>

int main()
{
    std::function<int(void)> func = []() -> int {
        for (int i = 0; i < 3; ++i) {
            printf("async is running\n");
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
        return 100;
    };
    auto f = std::async(func);
    int ret = f.get();
    printf("result is %d\n", ret);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注的罗哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值