std::future
Da 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 no cabeçalho <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) |
O
std::future
modelo de classe fornece um mecanismo para acessar o resultado de operações assíncronas: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.
- Uma operação assíncrona (criado através std::async, std::packaged_task, ou std::promise) pode fornecer um objeto
std::future
para o criador do que a operação assíncrona.Original:An asynchronous operation (created via std::async, std::packaged_task, or std::promise) can provide astd::future
object 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.
- O criador da operação assíncrona pode então usar uma variedade de métodos para consulta, aguarde, ou extrair um valor da
std::future
. Estes métodos podem bloquear se a operação assíncrona ainda não forneceu um 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.
- Quando a operação assíncrona está pronto para enviar um resultado para o criador, ele pode fazer isso modificando o estado compartilhado' (por exemplo std::promise::set_value) que está ligado a
std::future
do criador.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.
Note que as referências
std::future
compartilhada estado que não é compartilhado com todos os objetos assíncronos outras retorno (ao contrário 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] Funções de membro
constrói o 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. (função pública membro) | |
destrói o 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. (função pública membro) | |
move o 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. (função pública membro) | |
devolve uma shared_future referindo-se ao resultado, associado à *this Original: returns a shared_future referring to the result associated to *this The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função pública membro) | |
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. | |
devolve o 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. (função pública membro) | |
Original: State The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
Verifica se o futuro tem estado compartilhado com uma promessa 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. (função pública membro) | |
aguarda o resultado de se tornarem disponíveis 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. (função pública membro) | |
waits for the result, returns if it is not available for the specified timeout duration (função pública membro) | |
aguarda o resultado, retorna se ele não está disponível até que ponto de tempo especificado, foi atingido 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. (função pública membro) |
[editar] Exemplo
#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'; }
Saída:
Waiting...Done! Results are: 7 8 9
[editar] Veja também
(C++11) |
executa uma função de forma assíncrona (potencialmente em um novo segmento) e retorna uma std::future que vai segurar o 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. (modelo de função) |
(C++11) |
espera por um valor (possivelmente relacionado por outros futuros) que é definido de forma assíncrona 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. (modelo de classe) |