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
179
votes
4 answers

What is the difference between packaged_task and async

While working with the threaded model of C++11, I noticed that std::packaged_task task([](int a, int b) { return a + b; }); auto f = task.get_future(); task(2,3); std::cout << f.get() << '\n'; and auto f =…
nikolas
  • 8,707
  • 9
  • 50
  • 70
59
votes
2 answers

When to use promise over async or packaged_task?

When should I use std::promise over std::async or std::packaged_task? Can you give me practical examples of when to use each one of them?
Domingos Martins
  • 591
  • 1
  • 5
  • 4
16
votes
2 answers

std::packaged_task not breaking promises on destruction?

I'm encountering something very weird when using packaged tasks. When reading ~packaged_task I get the impression that if a std::packaged_task is destroyed before it is executed, the promise will be broken and an attempt to get the result from the…
Emily L.
  • 5,673
  • 2
  • 40
  • 60
9
votes
2 answers

How do I create a packaged_task with parameters?

Following this excellent tutorial for futures, promises and packaged tasks I got to the the point where I wanted to prepare my own task #include #include using namespace std; int ackermann(int m, int n) { // might take a…
towi
  • 21,587
  • 28
  • 106
  • 187
9
votes
1 answer

std::future still deferred when using std::packaged_task (VS11)

It seems that unless you call std::async a std::future will never be set to any other state than future_status::deferred unless you call get or wait on the future. wait_for & wait_until will continue to not block and return future_status::deferred…
8
votes
3 answers

Why std::future is different returned from std::packaged_task and std::async?

I got to know the reason that future returned from std::async has some special shared state through which wait on returned future happened in the destructor of future. But when we use std::pakaged_task, its future does not exhibit the same…
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
8
votes
1 answer

possible data race using packaged_task and threads

I recently ran valgrind --tool=helgrind on my project and got a warning "possible data race", which I thought was concerning. However, even this simple test program leads to this message: #include #include #include int…
stefan
  • 10,215
  • 4
  • 49
  • 90
8
votes
3 answers

Why is std::packaged_task not valid?

Using MSVC2012, The following code will compile and run as expected std::packaged_task< int() > task( []()->int{ std::cout << "hello world" << std::endl; return 0; } ); std::thread t( std::move(task) ); t.join(); while the following code will fail…
aCuria
  • 6,935
  • 14
  • 53
  • 89
7
votes
3 answers

How std::packaged_task works

I am analysing the following code snippet and am trying to understand it in detail: template auto ThreadPool::add( FUNCTION&& Function, ARGUMENTS&&... Arguments ) -> std::future
Martin Kopecký
  • 912
  • 5
  • 16
7
votes
2 answers

Implementing a simple, generic thread pool in C++11

I want to create a thread pool for experimental purposes (and for the fun factor). It should be able to process a wide variety of tasks (so I can possibly use it in later projects). In my thread pool class I'm going to need some sort of task…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
7
votes
1 answer

Is there a packaged_task::set_exception equivalent?

My assumption is that packaged_task has a promise underneath. If my task throws an exception, how do I route that to the associated future? With just a promise I could call set_exception – how do I do the same for packaged_task?
fbrereto
  • 35,429
  • 19
  • 126
  • 178
6
votes
1 answer

How should I correctly move packaged_task to lambda?

I have been trying to capture packaged_task into lambda, but I've failed. I understand move-semantics at all, also read some modern literature and I was thinking I didn't miss anything. Also I have read Josuttis's move semantics book, and ISO topic…
Max
  • 88
  • 6
6
votes
2 answers

What does this syntax mean, `class template class name`

I've been trying more about multi threaded programming in c++, and i was having difficulty understanding std::promise so i began searching for answers on this website, and low and behold, there is somebody with the same question as me. But reading…
GamefanA
  • 1,555
  • 2
  • 16
  • 23
5
votes
2 answers

Move packaged_task into lambda

I want to move and call a boost::packaged_task inside a lambda. However, I can't figure out an elegant solution. e.g. This won't compile. template auto begin_invoke(Func&& func) ->…
ronag
  • 49,529
  • 25
  • 126
  • 221
5
votes
1 answer

Is it safe to never retrieve the result of a std::future from a std::packaged_task?

Is it safe to create a std::future from a std::packaged_task, which executes on a separate thread, but not always retrieve its result? #include #include class Result { Result() {} ~Result() {} }; void foo() { …
Pol
  • 3,848
  • 1
  • 38
  • 55
1
2 3 4 5 6