std::async
Aus 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. |
definiert in Header <future> | ||
template<class Function, class... Args> std::future<typenamestd::result_of<Function(Args...)>::type> | (1) | (seit C++11) |
template<class Function, class... Args> std::future<typenamestd::result_of<Function(Args...)>::type> | (2) | (seit C++11) |
Die Template-Funktion
1) async
führt die Funktion f
asynchron (möglicherweise in einem separaten Thread) und gibt einen std::future, die schließlich halten wird das Ergebnis dieser Funktion Anruf .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.
Bewirkt das gleiche wie async(std::launch::async|std::launch::deferred, f, args...). Mit anderen Worten kann
2) f
in einem neuen Thread gestartet werden, oder sie kann synchron betrieben werden, wenn die resultierende std::future für einen Wert abgefragt wird .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.
Ruft eine Funktion
f
mit Argumenten args
nach einem bestimmten Start-Politik 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.
- Wenn die async Flag gesetzt ist (dh policy &std::launch::async!=0), dann
async
startet einen neuen Thread der Ausführung wie von std::thread(f, args...), außer dass, wenn die Funktionf
einen Wert zurückgibt oder eine Ausnahme auslöst, wird es in der gemeinsamen Staat zugänglich aufbewahrt durch die std::future, dassasync
zum Aufrufer zurückkehrt .Original:If the async flag is set (i.e. policy &std::launch::async!=0), thenasync
spawns a new thread of execution as if by std::thread(f, args...), except that if the functionf
returns a value or throws an exception, it is stored in the shared state accessible through the std::future thatasync
returns 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.
- Wenn die latenten Flag gesetzt ist (dh policy &std::launch::deferred!=0), dann
async
Konvertitenargs...
die gleiche Weise wie std::thread Konstruktor, aber nicht laichen einen neuen Thread. Stattdessen wird lazy evaluation durchgeführt: Der erste Anruf wird zu einer nicht-Zeitüberschreitung warten Funktion auf dem std::future, dassasync
an den Aufrufer zurückgegeben verursachenf(args...)
in den aktuellen Thread ausgeführt werden. Alle weiteren Zugriffen auf den gleichen std::future wird das Ergebnis sofort zurück .Original:If the deferred flag is set (i.e. policy &std::launch::deferred!=0), thenasync
convertsargs...
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 thatasync
returned 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.
- Wenn sowohl die std::launch::async und std::launch::deferred Flaggen in
policy
gesetzt sind, ist es an der Umsetzung, ob asynchrone Ausführung oder faul Auswertung durchführen .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.
Inhaltsverzeichnis |
[Bearbeiten]Parameter
f | - | Funktion oder Objekt zu nennen 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... | - | Parameter, die an f passierenOriginal: parameters to pass to f The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
policy | - | Bitmaskenwert, wo einzelne Bits steuern die zulässigen Methoden der Ausführung
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. |
[Bearbeiten]Rückgabewert
std::future bezogen auf den Rückgabewert der Funktion .
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.
[Bearbeiten]Ausnahmen
Wirft std::system_error mit Fehlerbedingung std::errc::resource_unavailable_try_again wenn der Start Politik ist std::launch::async und die Umsetzung ist nicht in der Lage, einen neuen Thread starten .
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.
[Bearbeiten]Notes
Die Implementierung kann das Verhalten des ersten Überlastung std::async indem zusätzliche (Implementierung definiert) Bits in der Standard-Start-Politik erweitern .
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.
[Bearbeiten]Beispiel
#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)returnstd::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