Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/packaged task"

From cppreference.com
< cpp‎ | thread
m (correct the spelling of "operator")
(normalizing example formatting)
Line 51: Line 51:
 
#include <future>
 
#include <future>
  
/**
+
void task_lambda()
* Use operator() to do the work
+
*/
+
void example1()
+
 
{
 
{
// wrap the function using lambda
+
    std::packaged_task<int(int,int)> task([](int a, int b) {
std::packaged_task<int(int,int)> task([](int _one,int _two) {
+
        return std::pow(a, b);  
return pow(_one, _two); });
+
    });
std::future<int> result = task.get_future();
+
    std::future<int> result = task.get_future();
 
 
task(2, 9);
+
    task(2, 9);
  
std::cout << "example1:" << std::endl;
+
    std::cout << "task_lambda:\t" << result.get() << '\n';
std::cout << result.get() << std::endl;
+
}
}//example1()
+
  
/**
+
void task_bind()
* Use thread to do the work.
+
* Remember to use move semantics, and pass the arguments.
+
*/
+
void example2()
+
 
{
 
{
std::packaged_task<int(int,int)> task(pow);
+
    std::packaged_task<int()> task(std::bind(std::pow, 2, 11));
auto result = task.get_future(); // auto -> std::future<int>
+
    std::future<int> result = task.get_future();
 
 
std::thread task_td(std::move(task), 2, 10);
+
    task();
task_td.join();
+
  
std::cout << "example2:" << std::endl;
+
    std::cout << "task_bind:\t" << result.get() << '\n';
std::cout << result.get() << std::endl;
+
}
}//example2()
+
  
/**
+
void task_thread()
* Use std::bind to bind arguments
+
*/
+
void example3()
+
 
{
 
{
std::packaged_task<int()> task(std::bind(pow, 2, 11));
+
    std::packaged_task<int(int,int)> task(std::pow);
auto result = task.get_future(); // auto -> std::future<int>
+
    std::future<int> result = task.get_future();
 
 
task(); // It will work the same for using thread
+
    std::thread task_td(std::move(task), 2, 10);
 +
    task_td.join();
  
std::cout << "example3:" << std::endl;
+
    std::cout << "task_thread:\t" << result.get() << '\n';
std::cout << result.get() << std::endl;
+
}
}//example3()
+
  
 
int main()
 
int main()
 
{
 
{
example1();
+
    task_lambda();
example2();
+
    task_bind();
example3();
+
    task_thread();
 
+
}
return 0;
+
}//main
+
 
+
 
| output=
 
| output=
example1:
+
task_lambda: 512
512
+
task_bind:   2048
example2:
+
task_thread: 1024
1024
+
example3:
+
2048
+
 
}}
 
}}
  

Revision as of 07:28, 7 February 2013

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
packaged_task
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <future>
</td>

<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>

<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

Template:cpp/thread/packaged task/dcl list constructorTemplate:cpp/thread/packaged task/dcl list destructorTemplate:cpp/thread/packaged task/dcl list operator=Template:cpp/thread/packaged task/dcl list validTemplate:cpp/thread/packaged task/dcl list swapTemplate:cpp/thread/packaged task/dcl list get futureTemplate:cpp/thread/packaged task/dcl list operator()Template:cpp/thread/packaged task/dcl list make ready at thread exitTemplate:cpp/thread/packaged task/dcl list reset
Getting the result
Execution

Non-member functions

Template:cpp/thread/packaged task/dcl list swap2

Helper classes

Template:cpp/thread/packaged task/dcl list uses allocator

Example

#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

See also

Template:cpp/thread/dcl list future