std::async
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <future>
|
||
| template< class Function, class... Args> std::future<typename std::result_of<Function(Args...)>::type> |
(1) | (C++11 起) |
| template< class Function, class... Args > std::future<typename std::result_of<Function(Args...)>::type> |
(2) | (C++11 起) |
。模板功能
1) asyncf异步运行的功能(可能在一个单独的线程),并返回一个std::future,最终将持有该函数调用的结果.Original:
The template function
async runs the function f asynchronously (potentially in a separate thread) and returns a std::future that will eventually hold the result of that function call.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
。的行为一样async(std::launch::async | std::launch::deferred, f, args...)。换句话说,
2) f可以开始在一个新的线程,或者它也可以同步运行的一个值被查询时,所得到的std::future.Original:
Behaves the same as async(std::launch::async | std::launch::deferred, f, args...). In other words,
f may be started in a new thread or it may be run synchronously when the resulting std::future is queried for a value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
。带参数的调用一个函数
fargs根据具体的上市政策policy。 Original:
Calls a function
f with arguments args according to a specific launch policy policy: The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 。如果“异步”标志的设置(即policy & std::launch::async != 0),然后
async产生一个新的线程执行,如果由std::thread(f, args...),但如果功能f返回一个值或抛出一个异常,它被存储在共享状态访问通过std::futureasync返回给调用者.Original:If the async flag is set (i.e. policy & std::launch::async != 0), thenasyncspawns a new thread of execution as if by std::thread(f, args...), except that if the functionfreturns a value or throws an exception, it is stored in the shared state accessible through the std::future thatasyncreturns to the caller.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 。如果“递延”标志(即policy & std::launch::deferred != 0),然后
async皈依args...相同的方式std::thread构造函数,但不会产生一个新的线程执行。相反,“懒惰的评价”:在第一次调用一个不定时等功能的std::futureasync返回给调用者会导致f(args...)在当前线程中执行。所有其他的访问相同的std::future将立即返回结果.Original:If the deferred flag is set (i.e. policy & std::launch::deferred != 0), thenasyncconvertsargs...the same way as by std::thread constructor, but does not spawn a new thread of execution. Instead, lazy evaluation is performed: the first call to a non-timed wait function on the std::future thatasyncreturned to the caller will causef(args...)to be executed in the current thread. All further accesses to the same std::future will return the result immediately.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 。 std::launch::async和std::launch::deferred标志设置在
policy,它的实施是否执行异步执行或懒惰的评价.Original:If both the std::launch::async and std::launch::deferred flags are set inpolicy, it is up to the implementation whether to perform asynchronous execution or lazy evaluation.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 。参数。
| f | - | 。到调用函数或函数对象。
Original: function or function object to call The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
| args... | - | 。参数传递给
f。Original: parameters to pass to fThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
| policy | - | 。位掩码值,其中个别的位控制在允许的执行方法
Original: bitmask value, where individual bits control the allowed methods of execution
The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
===。 返回值。===
。 std::future指的是该函数的返回值.
Original:
std::future referring to the return value of the function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 。例外。
。抛出std::system_error如果发射的政策是std::errc::resource_unavailable_try_again和实施是无法启动一个新线程的错误条件std::launch::async.
Original:
Throws std::system_error with error condition std::errc::resource_unavailable_try_again if the launch policy is std::launch::async and the implementation is unable to start a new thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 。注释。
。使额外的(实现定义的)位在默认启动的政策的实施可能会延长std::async第一个重载的行为.
Original:
The implementation may extend the behavior of the first overload of std::async by enabling additional (implementation-defined) bits in the default launch policy.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 。为例。
#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <future> template <typename RAIter> int parallel_sum(RAIter beg, RAIter end) { typename RAIter::difference_type 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'; }
Output:
The sum is 10000