Difference between revisions of "cpp/thread/packaged task"
From cppreference.com
m (correct the spelling of "operator") |
(normalizing example formatting) |
||
Line 51: | Line 51: | ||
#include <future> | #include <future> | ||
− | + | void task_lambda() | |
− | + | ||
− | + | ||
− | void | + | |
{ | { | ||
− | + | 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() | |
− | + | ||
− | + | ||
− | + | ||
− | void | + | |
{ | { | ||
− | + | std::packaged_task<int()> task(std::bind(std::pow, 2, 11)); | |
− | + | std::future<int> result = task.get_future(); | |
− | + | task(); | |
− | + | ||
− | + | std::cout << "task_bind:\t" << result.get() << '\n'; | |
− | + | } | |
− | } | + | |
− | + | void task_thread() | |
− | + | ||
− | + | ||
− | void | + | |
{ | { | ||
− | + | std::packaged_task<int(int,int)> task(std::pow); | |
− | + | 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() | int main() | ||
{ | { | ||
− | + | task_lambda(); | |
− | + | task_bind(); | |
− | + | task_thread(); | |
− | + | } | |
− | + | ||
− | } | + | |
− | + | ||
| output= | | output= | ||
− | + | task_lambda: 512 | |
− | 512 | + | task_bind: 2048 |
− | + | task_thread: 1024 | |
− | 1024 | + | |
− | + | ||
− | + | ||
}} | }} | ||
Revision as of 07:28, 7 February 2013
Template:ddcl list begin <tr class="t-dsc-header">
<td>Defined in header
</td>
<future>
<td></td> <td></td> </tr> <tr class="t-dcl ">
<td >template< class > class packaged_task; //not defined
</td>
<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >template< class R, class Args... >
class packaged_task<R(Args...)>;
</td>
class packaged_task<R(Args...)>;
<td > (2) </td> <td > (since C++11) </td> </tr> Template:ddcl list end
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 |
Member functions
Non-member functions
Helper classes
Example
Run this code
#include <iostream> #include <cmath> #include <thread> #include <future> 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(std::pow, 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(std::pow); 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