Questions tagged [packaged-task]

std::packaged_task<> is a class template introduced with the C++11 multi-threading functionality. This tag should be used for questions concerning the C++ class template and using it with C++ multi-threading and coroutines. The C++ Standard being used (C++11/14/17/20) should also be indicated. The class template is used to wrap a callable entity (lambda, function, etc.) to create an asynchronous task.

std::packaged_task<> is a class template introduced with the C++11 Standard that is part of the multi-threading functionality introduced with that version of the Standard. It requires the use of #include <future> to compile.

The purpose of std::packaged_task<> is to create an asynchronous task which can then be used with other C++ multi-threading library functionality to manage. You can use a std::future<> to get the result of the asynchronous task.

You can also use std::packaged_task<> along with its get_future() method with the proposed co_await operator of C++20 coroutines.

To use std::packaged_task<> requires two steps: create the packaged task object and call the object to start it running.

Additional reading

What is the difference between packaged_task and async

What is std::promise?

What is a lambda expression in C++11?

What are move semantics?

79 questions
2
votes
2 answers

How do I create a packaged_task with a member function?

On the program below, I'm trying to create a packaged_task with a member function: #include using namespace std; struct S { int calc(int& a) { return a*a; } }; int main() { S s; auto bnd = std::bind(&S::calc,…
user2286810
  • 136
  • 9
2
votes
1 answer

std::packaged_task with std::bind(func, this) within class

I have prepared a small snippet isolating my problem here. Here it is : #include #include #include class foo { public: foo(int a, int b) : m_a(a), m_b(b) {} int somefunc(int a, int b) {…
Yannick
  • 830
  • 7
  • 27
2
votes
1 answer

What is task in this example from cppreference?

In the example of this description of packaged_task from cppreference, a class named task appears. What is it? #include #include #include int main() { std::packaged_task task([](){return 7;}); // wrap the…
qdii
  • 12,505
  • 10
  • 59
  • 116
1
vote
2 answers

Constructing std::packaged_task from std::bind_front with a move-only bound argument does not compile

The following code uses std::bind_front to bind an object of move-only type as the first argument to a function, then constructs a std::packaged_task from the resulting function object. (Try it on Godbolt.) #include #include…
Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
1
vote
1 answer

How to tell std::future if std::promise exist in C++?

I create a std::packaged_task, push it to a std::list and return its std::future to users, like this: future_t emplace(...) { //... auto task = std::make_shared>( std::bind(std::forward(f),…
BBing
  • 152
  • 5
1
vote
1 answer

Is this threadpool code attempting double execution of task?

I have copied the below threadpool implementation from https://pastebin.com/MM5kSvH6. All looks good but i can't understand the logic at Line number 32 and Line number 71. Aren't both these lines trying to execute the function? I thought in…
user16551333
1
vote
1 answer

Crash on exit using C++0x threading library

I just started trying the new C++0x threading library, and I finally got several computation tasks run in parallel. The example takes one second to run, which is what I expected, but it crashes on exit. Any idea of what I'm doing wrong? #include…
pau.estalella
  • 2,197
  • 1
  • 15
  • 20
1
vote
0 answers

Moved captured std::packaged_task is suddenly const

As I want to give my async, reactor style pattern, callbacks a result in form of a future from boost::asio, I came across a compile error, i.e. constness clash I don't understand: When I want to call a packed task in intention to fulfill the…
Superlokkus
  • 4,731
  • 1
  • 25
  • 57
1
vote
2 answers

Create a packaged_task of function object

How to create a packaged_task of a function object ? For example, class TaskA { public: std::function func; TaskA(std::function&f) : func(f) {} int res; int operator()() { …
Debashish
  • 1,155
  • 19
  • 34
1
vote
1 answer

Add a std::packaged_task to an existing thread?

Is there an standard way to add a std::packaged_task to an existing thread? There's a nontrivial amount of overhead that must happen before the task is run, so I want to do that once, then keep the thread running and waiting for tasks to execute. I…
snips-n-snails
  • 637
  • 5
  • 22
1
vote
2 answers

using c++ packaged_task to construct producer-consumer pattern

I am trying to packaged_task to create a producer-consumer patten code is as following : test_thread9_producer1 and test_thread9_producer2 push task into a queue and test_thread9_consumer1 retrieve task from the queue to execute However when…
adam
  • 11
  • 2
1
vote
1 answer

Why does my thread invoke a function object instead of assigning its return value to my packaged_task’s future object?

I want learn how to retrieve the return value of a function using a packaged_task. In the code below I create a thread that runs my function DoTask. I then bind that function to packaged_task and get it to wait while I bind it to a packaged_task…
rtischer8277
  • 496
  • 6
  • 27
1
vote
0 answers

C++ unspecified future_errc value when packaged_task is wrapped in a thread, code 4

As the code below shows, I am trying to implement my own thread class by passing in a packaged_task. I then wait on the future from this packaged_task. I expect this code to wait 4 seconds and then I can get 0xcdcdcdcd from the future. #include…
MU Tong
  • 11
  • 2
1
vote
1 answer

Using std::bind to capture a parameter pack "by move"

I'm attempting to implement std::async from scratch, and have run into a hiccup with arguments of move-only type. The gist of it is, C++14 init-captures allow us to capture single variables "by move" or "by perfect forwarding", but they do not…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
1
vote
1 answer

Perfect forwarding and packaged_task wrapper

I'm trying to wrap packaged_task in a generic class, but having trouble initializing it with a generic function. I've gotten it to work for a specific one, but I want it to be more abstract. Just an fyi, if you uncomment the 2 lines of code I…
Dylan_Larkin
  • 503
  • 4
  • 15