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. |
Defined in header <future>
|
||
template< class Function, class... Args> std::future<typename std::result_of<Function(Args...)>::type> |
(1) | (desde C++11) |
template< class Function, class... Args > std::future<typename std::result_of<Function(Args...)>::type> |
(2) | (desde C++11) |
El
1) async
función de plantilla se ejecuta la función f
asíncrona (potencialmente en un hilo separado) y devuelve un std::future que eventualmente llevará a cabo el resultado de esa llamada a la función .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 comporta igual que async(std::launch::async | std::launch::deferred, f, args...). En otras palabras,
2) f
se puede iniciar en un nuevo hilo o se puede ejecutar sincrónicamente cuando el std::future resultante se consulta para un valor .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.
Llama a una función con argumentos
f
args
de acuerdo con una política de lanzamiento específica 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 la bandera' async se establece (es decir, policy & std::launch::async != 0), entonces
async
genera un nuevo hilo de ejecución como por std::thread(f, args...), excepto que si la funciónf
devuelve un valor o produce una excepción, que se almacena en el estado de acceso compartido a través de la std::future queasync
devuelve a la persona que llama .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 la bandera' diferido se activa (es decir, policy & std::launch::deferred != 0), entonces
async
convertidosargs...
la misma manera que por constructor std::thread, pero no generar un nuevo hilo de ejecución. En cambio, la evaluación perezosa' que se realiza la primera llamada a un no-programado esperar función en el std::future queasync
devuelve al llamador causaráf(args...)
que se ejecutará en el subproceso actual. Todos los accesos adicionales a la misma std::future devolverá el resultado inmediato .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 las banderas tanto de la std::launch::async y std::launch::deferred se establecen en
policy
, corresponde a la aplicación si desea realizar la ejecución asíncrona o evaluación perezosa .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.
Contenido |
[editar] Parámetros
f | - | objeto de función o la función a llamar
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... | - | parámetros que se pasan a
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 | - | valor de máscara de bits, donde cada bit controlar los métodos permitidos de ejecución
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. |
[editar] Valor de retorno
std::future referencia al valor devuelto por la función .
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.
[editar] Excepciones
Lanza std::system_error con std::errc::resource_unavailable_try_again condición de error si la política es std::launch::async lanzamiento y la puesta en práctica no puede iniciar un nuevo hilo .
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.
[editar] Notas
La implementación puede extender el comportamiento de la primera sobrecarga de std::async al permitir adicionales (definidos por la implementación) bits en la política de inicio predeterminada .
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.
[editar] Ejemplo
#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