std::future - cppreference.com
De cppreference.com
<tbody> </tbody>
| Definido no cabeçalho <future> |
||
|
|
(1) | (desde C++11) |
|
|
(2) | (desde C++11) |
|
|
(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.
Uma operação assíncrona (criado através std::async, std::packaged_task, ou std::promise) pode fornecer um objeto
std::futurepara 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 a
std::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.
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 the
std::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 astd::futuredo 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.
Funções de membro
constrói o objeto futuro Original: constructs the future object The text has been machine-translated via Google Translate. (função pública membro) [edit] | |
destrói o objeto futuro Original: destructs the future object The text has been machine-translated via Google Translate. (função pública membro) [edit] | |
move o objeto futuro Original: moves the future object The text has been machine-translated via Google Translate. (função pública membro) [edit] | |
devolve uma shared_future referindo-se ao resultado, associado à Original: The text has been machine-translated via Google Translate. (função pública membro) [edit] | |
Obtendo o resultado Original: Getting the result The text has been machine-translated via Google Translate. | |
devolve o resultado Original: returns the result The text has been machine-translated via Google Translate. (função pública membro) [edit] | |
Estado Original: State The text has been machine-translated via Google Translate. | |
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. (função pública membro) [edit] | |
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. (função pública membro) [edit] | |
| waits for the result, returns if it is not available for the specified timeout duration (função pública membro) [edit] | |
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. (função pública membro) [edit] | |
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
Veja também
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. (modelo de função) [edit] | |
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. (modelo de classe) [edit] | |