std::packaged_task
Материал из 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. |
| Заголовочный файл <future>
|
||
| template< class > class packaged_task; //not defined |
(1) | (начиная с C++11) |
| template< class R, class Args... > class packaged_task<R(Args...)>; |
(2) | (начиная с C++11) |
std::packaged_task шаблон класса обертывания любой вызываемый цели (функции, лямбда-выражения, связывают выражение или другой функции объекта), так что он может быть вызван асинхронно, и его значение возврата или исключение хранятся в общей государства, которые могут быть доступны через std::future Объекты.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.
Так же, как std::function,
std::packaged_task является полиморфным, распределитель-Aware контейнера: хранить вызываемые цели может быть выделено на куче или с предоставлена распределитель.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.
[править] Член функций
| строит объект задачи 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. (public функция-член) | |
| разрушает объект задачи 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. (public функция-член) | |
| перемещает объект задачи 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. (public функция-член) | |
| проверяет, является ли объект задачи имеет действительную функцию 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. (public функция-член) | |
| свопы двух объектов задачу 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. (public функция-член) | |
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. | |
| возвращает std::future связанные с обещанный результат 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. (public функция-член) | |
Original: Execution The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| выполняет функцию 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. (public функция-член) | |
| выполняет функцию обеспечения того, чтобы результат готов только один раз текущий поток существует 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. (public функция-член) | |
| Сбрасывает состояние отказа от любых хранятся результаты предыдущих казней 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. (public функция-член) | |
[править] Не являющиеся членами функций
| Специализируется 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. (шаблон функции) | |
[править] Вспомогательные классы
| Специализируется черта 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. (специализация шаблона класса) | |
[править] Пример
#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'; }
Вывод:
Waiting...Done! Result is 7
[править] См. также
| (C++11) |
ожидает значение, которое устанавливается в асинхронном режиме 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. (шаблон класса) |