std::packaged_task
From cppreference.com
 
 
 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Concurrency support library 
 
 
 
 
 
 
 
Cooperative cancellation  
 
 
 
 
 
 
 
 
 
 
 
 
 
 Mutual exclusion  Generic lock management  
 
 
 
 
 
 
 
 
 
 
 Condition variables  Semaphores  Latches and Barriers  
 
 
 
 Futures  
 
 
 
 
 
 Safe reclamation  Hazard pointers  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
 
(not defined)
 
 
 
 
 
 
 specializes the std::uses_allocator  type trait 
(class template specialization) [edit]
 
 
 
 
 
 
 
 
 
 
 (C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
(C++26)
(C++20)
(C++26)
  (C++20)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
  (C++11)
(C++11)
(C++17)
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)(C++20)
(C++20)
(C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++26)
(C++26)
(C++26)
(C++11)
(C++20)
(C++11)
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
(C++11)
(C++11)
(C++11)(deprecated in C++26)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++26)(C++26)
(C++26)(C++26)
(C++11)
(C++20)(C++20)
(C++20)
(C++20)
(C++11)(C++11)
(C++11)(C++11)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
std::packaged_task
 
 Member functions
 Getting the result
 Execution
 Non-member functions
 Helper classes
(until C++17)
 Deduction guides (C++17)
Defined in header 
 
 
<future> 
 template< class >
class packaged_task;
 (1) 
 (since C++11) class packaged_task;
(not defined)
template< class R, class ...ArgTypes > 
class packaged_task<R(ArgTypes...)>;
 (2) 
 (since C++11) 
class packaged_task<R(ArgTypes...)>;
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. Its return value or exception thrown is stored in a shared state which can be accessed through std::future  objects.
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.
Contents
[edit] Member functions
Getting the result
Execution
 
 executes the function ensuring that the result is ready only once the current thread exits 
(public member function) [edit]
(public member function) [edit]
 
 resets the state abandoning any stored results of previous executions 
(public member function) [edit]
(public member function) [edit]
[edit] Non-member functions
[edit] Helper classes
(C++11) (until C++17)
(class template specialization) [edit]
[edit] Deduction guides (since C++17)
[edit] Example
Run this code
#include <cmath> #include <functional> #include <future> #include <iostream> #include <thread> // unique function to avoid disambiguating the std::pow overload set int f(int x, int y) { return std::pow (x, y); } void task_lambda() { std::packaged_task<int(int, int)> task([](int a, int b) { return std::pow (a, b); }); std::future <int> result = task.get_future(); task(2, 9); std::cout << "task_lambda:\t" << result.get() << '\n'; } void task_bind() { std::packaged_task<int()> task(std::bind (f, 2, 11)); std::future <int> result = task.get_future(); task(); std::cout << "task_bind:\t" << result.get() << '\n'; } void task_thread() { std::packaged_task<int(int, int)> task(f); std::future <int> result = task.get_future(); std::thread task_td(std::move(task), 2, 10); task_td.join(); std::cout << "task_thread:\t" << result.get() << '\n'; } int main() { task_lambda(); task_bind(); task_thread(); }
Output:
task_lambda: 512 task_bind: 2048 task_thread: 1024
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior | 
|---|---|---|---|
| LWG 3117 | C++17 | deduction guides for packaged_taskwere missing | added |