std::future
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. |
| Definido en la cabecera <future>
|
||
| template< class T > class future; |
(1) | (desde C++11) |
| template< class T > class future<T&>; |
(2) | (desde C++11) |
| template<> class future<void>; |
(3) | (desde C++11) |
La plantilla
std::future clase proporciona un mecanismo para acceder al resultado de las operaciones asincrónicas:Original:
The class template
std::future provides a mechanism to access the result of asynchronous operations: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.
- Una operación asincrónica (creado por std::async, std::packaged_task o std::promise) puede proporcionar un objeto
std::futureal creador de esa operación asíncrona .Original:An asynchronous operation (created via std::async, std::packaged_task, or std::promise) can provide astd::futureobject to the creator of that asynchronous operation.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- El creador de la operación asincrónica se puede usar una variedad de métodos para consultar, esperar, o extraer un valor de la
std::future. Estos métodos pueden bloquear si la operación asincrónica no ha proporcionado aún un valor .Original:The creator of the asynchronous operation can then use a variety of methods to query, wait for, or extract a value from thestd::future. These methods may block if the asynchronous operation has not yet provided a value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Cuando la operación asincrónica está listo para enviar el resultado al creador, puede hacerlo modificando el estado compartido' (por ejemplo std::promise::set_value) que está vinculado a
std::futuredel creador .Original:When the asynchronous operation is ready to send a result to the creator, it can do so by modifying shared state (e.g. std::promise::set_value) that is linked to the creator'sstd::future.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tenga en cuenta que las referencias
std::future estado compartido que no es compartida con ningún objeto de devolución asincrónica otros (a diferencia de std::shared_future) . Original:
Note that
std::future references shared state that is not shared with any other asynchronous return objects (as opposed to std::shared_future). 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] Las funciones miembro
| construye el objeto futuro Original: constructs the future object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
| destructs el objeto futuro Original: destructs the future object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
| mueve el objeto futuro Original: moves the future object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
| devuelve una referencia a shared_future el resultado asociado a *thisOriginal: returns a shared_future referring to the result associated to *thisThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
Original: Getting the result The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| devuelve el resultado Original: returns the result The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
Original: State The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| Comprueba si el futuro ha estado compartido con una promesa Original: checks if the future has shared state with a promise The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
| espera a que el resultado esté disponible Original: waits for the result to become available The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
| waits for the result, returns if it is not available for the specified timeout duration (función miembro público) | |
| espera el resultado, devuelve si no está disponible hasta que el punto de tiempo especificado ha sido alcanzado Original: waits for the result, returns if it is not available until specified time point has been reached The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (función miembro público) | |
[editar] Ejemplo
#include <iostream> #include <future> #include <thread> int main() { // future from a packaged_task std::packaged_task<int()> task([](){ return 7; }); // wrap the function std::future<int> f1 = task.get_future(); // get a future std::thread(std::move(task)).detach(); // launch on a thread // future from an async() std::future<int> f2 = std::async(std::launch::async, [](){ return 8; }); // future from a promise std::promise<int> p; std::future<int> f3 = p.get_future(); std::thread( [](std::promise<int>& p){ p.set_value(9); }, std::ref(p) ).detach(); std::cout << "Waiting..."; f1.wait(); f2.wait(); f3.wait(); std::cout << "Done!\nResults are: " << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n'; }
Salida:
Waiting...Done! Results are: 7 8 9
[editar] Ver también
| (C++11) |
ejecuta una función asíncrona (posiblemente en un nuevo hilo) y devuelve un std::future que contendrá el resultado Original: runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (plantilla de función) |
| (C++11) |
espera a un valor (posiblemente por referencia por otros futuros) que se establece de forma asincrónica Original: waits for a value (possibly referenced by other futures) that is set asynchronously The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (plantilla de clase) |

