std::async
De 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. |
Déclaré dans l'en-tête <future> | ||
template<class Function, class... Args> std::future<typenamestd::result_of<Function(Args...)>::type> | (1) | (depuis C++11) |
template<class Function, class... Args> std::future<typenamestd::result_of<Function(Args...)>::type> | (2) | (depuis C++11) |
Le
1) async
fonction de modèle exécute la fonction f
asynchrone (potentiellement dans un thread séparé) et renvoie une std::future qui finira par contenir le résultat de cet appel de fonction .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.
Se comporte comme async(std::launch::async|std::launch::deferred, f, args...). En d'autres termes,
2) f
peut être démarré dans un nouveau thread ou il peut être exécuté de façon synchrone lorsque le std::future résultant est interrogé pour une valeur .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.
Appelle une fonction avec des arguments
f
args
selon une politique de lancement spécifique 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.
- Si le' async drapeau est positionné (c.-à-policy &std::launch::async!=0), puis
async
génère un nouveau thread d'exécution comme par std::thread(f, args...), sauf que si la fonction renvoie une valeurf
ou lève une exception, elle est stockée dans l'état partagé accessible à travers le std::future queasync
retourne à l'appelant .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.
- Si le drapeau' différé est activé (c'est à dire policy &std::launch::deferred!=0), puis convertis
async
args...
la même manière que par le constructeur std::thread, mais ne lance pas un nouveau thread d'exécution. Au lieu de cela,' évaluation paresseuse est réalisée: le premier appel à un non-chronométré attendre fonction sur le std::future queasync
renvoyé à l'appelant feraf(args...)
être exécuté dans le thread courant. Tous les accès supplémentaires à la même std::future retournera le résultat immédiatement .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.
- Si drapeaux à la fois le std::launch::async et std::launch::deferred sont mis en
policy
, c'est à la mise en œuvre s'il faut effectuer l'exécution asynchrone ou l'évaluation paresseuse .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.
Sommaire |
[modifier]Paramètres
f | - | objet fonction ou la fonction à appeler 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... | - | paramètres à passer au f Original: 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 | - | valeur masque, où chaque bit de contrôler les méthodes autorisées d'exécution
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. |
[modifier]Retourne la valeur
std::future se référant à la valeur de retour de la fonction .
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.
[modifier]Exceptions
Lance std::system_error avec std::errc::resource_unavailable_try_again condition d'erreur si la politique de lancement est std::launch::async et la mise en œuvre est incapable de démarrer un nouveau thread .
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.
[modifier]Notes
La mise en œuvre peut étendre le comportement de la première surcharge de std::async en permettant à d'autres (définie par l'implémentation) bits dans la politique de lancement par défaut .
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.
[modifier]Exemple
#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';}
Résultat :
The sum is 10000