Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | thread
m
m (fmt, headers sorted)
 
(19 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 
{{cpp/title|packaged_task}}
 
{{cpp/title|packaged_task}}
 
{{cpp/thread/packaged_task/navbar}}
 
{{cpp/thread/packaged_task/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | future}}
+
{{dcl header|future}}
{{ddcl list item | num=1 | notes={{mark since c++11}} |
+
{{dcl|num=1|since=c++11|
 
template< class > class packaged_task; //not defined
 
template< class > class packaged_task; //not defined
 
}}
 
}}
{{ddcl list item | num=2 | notes={{mark since c++11}} |
+
{{dcl|num=2|since=c++11|
template< class R, class Args... >  
+
template< class R, class ...ArgTypes >  
class packaged_task<R(Args...)>;
+
class packaged_task<R(ArgTypes...)>;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
The class template {{tt|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 {{c|std::future}} objects.
+
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.
  
Just like {{c|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.
+
{{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.
 +
}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/thread/packaged_task/dcl list constructor}}
+
{{dsc inc|cpp/thread/packaged_task/dsc constructor}}
{{dcl list template | cpp/thread/packaged_task/dcl list destructor}}
+
{{dsc inc|cpp/thread/packaged_task/dsc destructor}}
{{dcl list template | cpp/thread/packaged_task/dcl list operator{{=}}}}
+
{{dsc inc|cpp/thread/packaged_task/dsc operator{{=}}}}
{{dcl list template | cpp/thread/packaged_task/dcl list valid}}
+
{{dsc inc|cpp/thread/packaged_task/dsc valid}}
{{dcl list template | cpp/thread/packaged_task/dcl list swap}}
+
{{dsc inc|cpp/thread/packaged_task/dsc swap}}
  
{{dcl list h2 | Getting the result}}
+
{{dsc h2|Getting the result}}
{{dcl list template | cpp/thread/packaged_task/dcl list get_future}}
+
{{dsc inc|cpp/thread/packaged_task/dsc get_future}}
  
{{dcl list h2 | Execution}}
+
{{dsc h2|Execution}}
{{dcl list template | cpp/thread/packaged_task/dcl list operator()}}
+
{{dsc inc|cpp/thread/packaged_task/dsc operator()}}
{{dcl list template | cpp/thread/packaged_task/dcl list make_ready_at_thread_exit}}
+
{{dsc inc|cpp/thread/packaged_task/dsc make_ready_at_thread_exit}}
{{dcl list template | cpp/thread/packaged_task/dcl list reset}}
+
{{dsc inc|cpp/thread/packaged_task/dsc reset}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Non-member functions===
 
===Non-member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/thread/packaged_task/dcl list swap2}}
+
{{dsc inc|cpp/thread/packaged_task/dsc swap2}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Helper classes===
 
===Helper classes===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/thread/packaged_task/dcl list uses_allocator}}
+
{{dsc inc|cpp/thread/packaged_task/dsc uses_allocator}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
==={{rl|deduction_guides|Deduction guides}}{{mark since c++17}}===
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
#include <iostream>
+
 
#include <cmath>
 
#include <cmath>
#include <thread>
+
#include <functional>
 
#include <future>
 
#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()
* Use opeartor() 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 pow(_one, _two); });
+
        return std::pow(a, b);  
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(f, 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(f);
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();
 +
}
 +
|output=
 +
task_lambda: 512
 +
task_bind:  2048
 +
task_thread: 1024
 +
}}
  
return 0;
+
===Defect reports===
}//main
+
{{dr list begin}}
 
+
{{dr list item|wg=lwg|dr=3117|std=C++17|before=deduction guides for {{tt|packaged_task}} were missing|after=added}}
| output=
+
{{dr list end}}
example1:
+
512
+
example2:
+
1024
+
example3:
+
2048
+
}}
+
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/thread/dcl list future}}
+
{{dsc inc|cpp/thread/dsc future}}
{{dcl list end}}
+
{{dsc end}}
  
[[de:cpp/thread/packaged task]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/thread/packaged task]]
+
[[fr:cpp/thread/packaged task]]
+
[[it:cpp/thread/packaged task]]
+
[[ja:cpp/thread/packaged task]]
+
[[pt:cpp/thread/packaged task]]
+
[[ru:cpp/thread/packaged task]]
+
[[zh:cpp/thread/packaged task]]
+

Latest revision as of 02:02, 23 October 2023

 
 
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
 
 
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, std::packaged_task is a polymorphic, allocator-aware container: the stored callable target may be allocated on heap or with a provided allocator.

(until C++17)

Contents

[edit] Member functions

constructs the task object
(public member function) [edit]
destructs the task object
(public member function) [edit]
moves the task object
(public member function) [edit]
checks if the task object has a valid function
(public member function) [edit]
swaps two task objects
(public member function) [edit]
Getting the result
returns a std::future associated with the promised result
(public member function) [edit]
Execution
executes the function
(public member function) [edit]
executes the function ensuring that the result is ready only once the current thread exits
(public member function) [edit]
resets the state abandoning any stored results of previous executions
(public member function) [edit]

[edit] Non-member functions

specializes the std::swap algorithm
(function template) [edit]

[edit] Helper classes

specializes the std::uses_allocator type trait
(class template specialization) [edit]

[edit] Deduction guides(since C++17)

[edit] Example

#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) [edit]