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
:传递给 发的参数。- 返回值: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;
}