Difference between revisions of "cpp/thread/packaged task"
From cppreference.com
m (link Callable) |
Andreas Krug (Talk | contribs) m (fmt, headers sorted) |
||
(7 intermediate revisions by 6 users not shown) | |||
Line 2: | Line 2: | ||
{{cpp/thread/packaged_task/navbar}} | {{cpp/thread/packaged_task/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | future}} | + | {{dcl header|future}} |
− | {{dcl | num=1 | since=c++11 | | + | {{dcl|num=1|since=c++11| |
template< class > class packaged_task; //not defined | template< class > class packaged_task; //not defined | ||
}} | }} | ||
− | {{dcl | num=2 | since=c++11 | | + | {{dcl|num=2|since=c++11| |
− | template< class R, class ... | + | template< class R, class ...ArgTypes > |
− | class packaged_task<R( | + | class packaged_task<R(ArgTypes...)>; |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | The class template {{tt|std::packaged_task}} wraps any {{ | + | The class template {{tt|std::packaged_task}} wraps any {{named req|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 {{lc|std::future}} objects. |
+ | {{rrev|until=c++17| | ||
Just like {{lc|std::function}}, {{tt|std::packaged_task}} is a polymorphic, allocator-aware container: the stored callable target may be allocated on heap or with a provided allocator. | Just like {{lc|std::function}}, {{tt|std::packaged_task}} is a polymorphic, allocator-aware container: the stored callable target may be allocated on heap or with a provided allocator. | ||
+ | }} | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/thread/packaged_task/dsc constructor}} | + | {{dsc inc|cpp/thread/packaged_task/dsc constructor}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc destructor}} | + | {{dsc inc|cpp/thread/packaged_task/dsc destructor}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc operator{{=}}}} | + | {{dsc inc|cpp/thread/packaged_task/dsc operator{{=}}}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc valid}} | + | {{dsc inc|cpp/thread/packaged_task/dsc valid}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc swap}} | + | {{dsc inc|cpp/thread/packaged_task/dsc swap}} |
− | {{dsc h2 | Getting the result}} | + | {{dsc h2|Getting the result}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc get_future}} | + | {{dsc inc|cpp/thread/packaged_task/dsc get_future}} |
− | {{dsc h2 | Execution}} | + | {{dsc h2|Execution}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc operator()}} | + | {{dsc inc|cpp/thread/packaged_task/dsc operator()}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc make_ready_at_thread_exit}} | + | {{dsc inc|cpp/thread/packaged_task/dsc make_ready_at_thread_exit}} |
− | {{dsc inc | cpp/thread/packaged_task/dsc reset}} | + | {{dsc inc|cpp/thread/packaged_task/dsc reset}} |
{{dsc end}} | {{dsc end}} | ||
===Non-member functions=== | ===Non-member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/thread/packaged_task/dsc swap2}} | + | {{dsc inc|cpp/thread/packaged_task/dsc swap2}} |
{{dsc end}} | {{dsc end}} | ||
===Helper classes=== | ===Helper classes=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/thread/packaged_task/dsc uses_allocator}} | + | {{dsc inc|cpp/thread/packaged_task/dsc uses_allocator}} |
{{dsc end}} | {{dsc end}} | ||
+ | |||
+ | ==={{rl|deduction_guides|Deduction guides}}{{mark since c++17}}=== | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | | code= | + | |code= |
− | + | ||
#include <cmath> | #include <cmath> | ||
− | |||
− | |||
#include <functional> | #include <functional> | ||
+ | #include <future> | ||
+ | #include <iostream> | ||
+ | #include <thread> | ||
// unique function to avoid disambiguating the std::pow overload set | // unique function to avoid disambiguating the std::pow overload set | ||
− | int f(int x, int y) { return std::pow(x,y); } | + | int f(int x, int y) { return std::pow(x, y); } |
void task_lambda() | void task_lambda() | ||
{ | { | ||
− | std::packaged_task<int(int,int)> task([](int a, int b) { | + | std::packaged_task<int(int, int)> task([](int a, int b) |
+ | { | ||
return std::pow(a, b); | return std::pow(a, b); | ||
}); | }); | ||
Line 79: | Line 84: | ||
void task_thread() | void task_thread() | ||
{ | { | ||
− | std::packaged_task<int(int,int)> task(f); | + | std::packaged_task<int(int, int)> task(f); |
std::future<int> result = task.get_future(); | std::future<int> result = task.get_future(); | ||
Line 94: | Line 99: | ||
task_thread(); | task_thread(); | ||
} | } | ||
− | | output= | + | |output= |
task_lambda: 512 | task_lambda: 512 | ||
task_bind: 2048 | task_bind: 2048 | ||
task_thread: 1024 | task_thread: 1024 | ||
}} | }} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=3117|std=C++17|before=deduction guides for {{tt|packaged_task}} were missing|after=added}} | ||
+ | {{dr list end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/thread/dsc future}} | + | {{dsc inc|cpp/thread/dsc future}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 02:02, 23 October 2023
Defined in header <future>
|
||
template< class > class packaged_task; //not defined |
(1) | (since C++11) |
template< class R, class ...ArgTypes > class packaged_task<R(ArgTypes...)>; |
(2) | (since C++11) |
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, |
(until C++17) |
Contents |
[edit] Member functions
constructs the task object (public member function) | |
destructs the task object (public member function) | |
moves the task object (public member function) | |
checks if the task object has a valid function (public member function) | |
swaps two task objects (public member function) | |
Getting the result | |
returns a std::future associated with the promised result (public member function) | |
Execution | |
executes the function (public member function) | |
executes the function ensuring that the result is ready only once the current thread exits (public member function) | |
resets the state abandoning any stored results of previous executions (public member function) |
[edit] Non-member functions
specializes the std::swap algorithm (function template) |
[edit] Helper classes
(C++11) (until C++17) |
specializes the std::uses_allocator type trait (class template specialization) |
[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_task were missing
|
added |
[edit] See also
(C++11) |
waits for a value that is set asynchronously (class template) |