首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::async

Defined in header <future>

?

?

?

(1)

?

template< class Function, class... Args> std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>> async( Function&& f, Args&&... args );

(since C++11) (until C++17)

template< class Function, class... Args> std::future<std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>> async( Function&& f, Args&&... args );

(since C++17)

?

(2)

?

template< class Function, class... Args > std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>> async( std::launch policy, Function&& f, Args&&... args );

(since C++11) (until C++17)

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 );

(since C++17)

模板函数async运行函数f异步%28可能位于单独的线程中,该线程可能是线程池%29的一部分,并返回std::future这将最终保存该函数调用的结果。

1%29的行为与async(std::launch::async|std::launch::deferred, f, args...)换句话说,f可能在另一个线程中执行,或者当生成的std::future查询值。

2%29调用函数f有论据args根据具体的发射政策policy*

  • 如果异步标志设置为%28i.e。policy &std::launch::async!=0%29,那么async执行可调用对象。f在执行%28的新线程上,所有线程-局部变量初始化%29,就像由std::thread(std::forward<F>(f),std::forward<Args>(args)...),但如果函数f返回值或抛出异常,则将其存储在通过std::futureasync返回给来电者。
  • 如果递延标志设置为%28i.e。policy &std::launch::deferred!=0%29,那么async皈依fargs...以同样的方式std::thread构造函数,但不生成新的执行线程。相反,懒惰评价执行:对非定时等待函数的第一次调用。std::futureasync返回给调用方将导致f作为rvalue%29调用%28,其副本为args...%28还在当前线程%28中以rvalue%29的形式传递,该线程不必是最初调用的线程。std::async29%。结果或异常被置于与未来相关联的共享状态中,只有这样才能使其就绪。所有进一步访问相同的std::future将立即返回结果。
  • 如果两个std::launch::asyncstd::launch::deferred标志设置在policy,执行异步执行还是延迟评估取决于实现。

If neither std::launch::async nor std::launch::deferred, nor any implementation-defined policy flag is set in policy, the behavior is undefined.

(since C++14)

  • 如果两者都没有std::launch::async也不std::launch::deferred中未设置任何实现定义的策略标志。policy,该行为是未定义的。

%28自C++14%29

在任何情况下,std::async同步性中定义的28名ASstd::memory_order%29f,以及完成f排序-前使共享状态就绪。如果async策略,则关联的线程完成。同步性从等待共享状态的第一个函数或释放共享状态的最后一个函数返回的成功返回,两者以前面的哪个为准。

参数

f

-

Callable object to call

args...

-

parameters to pass to f

policy

-

bitmask value, where individual bits control the allowed methods of execution Bit Explanation std::launch::async enable asynchronous evaluation std::launch::deferred enable lazy evaluation

Bit

Explanation

std::launch::async

enable asynchronous evaluation

std::launch::deferred

enable lazy evaluation

Bit

Explanation

std::launch::async

enable asynchronous evaluation

std::launch::deferred

enable lazy evaluation

类型要求

功能,ARG必须符合可移动建筑的要求。

返回值

std::future引用此调用创建的共享状态std::async...

例外

抛出std::system_error有误差条件std::errc::resource_unavailable_try_again如果启动策略等于std::launch::async如果策略是async|deferred或者设置了其他位,它将返回到延迟或实现定义的策略(本例中为%29),或者std::bad_alloc如果无法分配内部数据结构的内存。

注记

的第一个重载的行为。std::async通过在默认启动策略中启用额外的%28实现定义%29位。

实现定义的启动策略的示例是同步策略%28立即执行,在异步调用%29中,任务策略%28与异步类似,但是线程局部变量没有清除%29。

如果std::futurestd::async对象的析构函数未从引用移动或绑定到引用。std::future将在完整表达式的末尾阻塞,直到异步操作完成为止,本质上是生成代码,如以下同步代码:

二次

代码语言:javascript
复制
std::async(std::launch::async, []{ f(); }); // temporary's dtor waits for f()
std::async(std::launch::async, []{ g(); }); // does not start until f() completes

二次

%28请注意,通过调用std::异步以外的其他方式获得的std::期货的析构函数从不阻止%29。

缺陷报告

以下行为更改缺陷报告追溯应用于先前发布的C++标准。

DR

Applied to

Behavior as published

Correct behavior

LWG 2021

C++11

return type incorrect and value category of arguments unclear in the deferred case

corrected return type and clarified that rvalues are used

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <future>
 
template <typename RAIter>
int parallel_sum(RAIter beg, RAIter end)
{
    auto len = end - beg;
    if(len < 1000)
        return std::accumulate(beg, end, 0);
 
    RAIter mid = beg + len/2;
    auto handle = std::async(std::launch::async,
                             parallel_sum<RAIter>, mid, end);
    int sum = parallel_sum(beg, mid);
    return sum + handle.get();
}
 
int main()
{
    std::vector<int> v(10000, 1);
    std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
The sum is 10000

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com