std::packaged_task
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 > class packaged_task; //not defined |
(1) | (desde C++11) |
template< class R, class Args... > class packaged_task<R(Args...)>; |
(2) | (desde C++11) |
O modelo
std::packaged_task
classe envolve qualquer alvo exigível (função, lambda expressão, expressão bind, ou outro objeto de função), de modo que ele pode ser chamado de forma assíncrona, e seu valor de retorno ou exceção lançada é armazenado no estado compartilhado, que pode ser acessado através std::future objetos.Original:
The class template
std::packaged_task
wraps any callable target (function, lambda expression, bind expression, or another function object) so that it can be invoked asynchronously, and its return value or exception thrown is stored in the shared state, which can be accessed through std::future objects.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.
Assim como std::function,
std::packaged_task
é um polimórfico, recipiente alocador-ciente: o alvo armazenados exigível pode ser atribuído, pilha ou com um alocador desde.Original:
Just like std::function,
std::packaged_task
is a polymorphic, allocator-aware container: the stored callable target may be allocated on heap or with a provided allocator.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 de tarefa Original: constructs the task 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 de tarefa Original: destructs the task 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 de tarefa Original: moves the task 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) | |
Verifica se o objeto tarefa tem uma função válida Original: checks if the task object has a valid function 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) | |
troca dois objetos de tarefas Original: swaps two task objects 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. | |
retorna um std::future associado ao resultado prometido Original: returns a std::future associated with the promised 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: Execution The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
executa a função Original: executes the function 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) | |
executa a função de garantir que o resultado é preparada uma única vez a thread corrente Original: executes the function ensuring that the result is ready only once the current thread exits 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) | |
redefine o estado abandonar quaisquer resultados armazenados de execuções anteriores Original: resets the state abandoning any stored results of previous executions 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] Não-membros funções
o algoritmo especializado std::swap Original: specializes the std::swap algorithm 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) |
[editar] Classes auxiliares
especializa o traço tipo std::uses_allocator Original: specializes the std::uses_allocator type trait The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (especialização modelo. classe) |
[editar] Exemplo
#include <iostream> #include <future> #include <thread> int main() { std::packaged_task<int()> task([](){return 7;}); // wrap the function std::future<int> result = task.get_future(); // get a future std::thread(std::move(task)).detach(); // launch on a thread std::cout << "Waiting..."; result.wait(); std::cout << "Done!\nResult is " << result.get() << '\n'; }
Saída:
Waiting...Done! Result is 7
[editar] Veja também
(C++11) |
espera por um valor que é definido de forma assíncrona Original: waits for a value 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) |