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

How do I create a queue that holds boost::packaged_task<> with functions that return arbitrary types?

I'm trying to construct a work queue of functions that need to be executed by one thread and can be fed by many threads. To accomplish this, I was planning on using the boost::packaged_task and boost::unique_future. The idea would be you would…
Abe Schneider
  • 977
  • 1
  • 11
  • 23
4
votes
1 answer

Why does `packaged_task` not have deduction guides?

I was naively expecting this to compile: template auto run(Func && func) { auto package = std::packaged_task{std::forward(func)}; // deduce the template args automatically (C++17) auto future = package.get_future(); …
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
4
votes
2 answers

Why require copy constructor for packaged_task in VS

class MoveOnlyOperation { public: MoveOnlyOperation() = default; MoveOnlyOperation(const MoveOnlyOperation&) = delete; MoveOnlyOperation(MoveOnlyOperation&&) = default; int operator()() { …
Ghita
  • 4,465
  • 4
  • 42
  • 69
4
votes
3 answers

Does `std::packaged_task` need a CopyConstructible constructor argument?

I have this minimal not-working example of code #include int main() { auto intTask = std::packaged_task( []()->int{ return 5; } ); std::packaged_task voidTask{ std::move(intTask) }; } Why doesn't it compile (on gcc…
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
4
votes
2 answers

wrapping std::packaged_task inside a custom class

I'm trying to wrap std::packaged_task inside another class in order to be used together with a task scheduler. At the moment I got it all working except std::future support. To get std::future support I figured out I need to use std::packaged_task…
Edward A
  • 2,291
  • 2
  • 18
  • 31
4
votes
1 answer

Anonymous std::packaged_task

i am trying to build some wrapper around Glib::Dispatcher to dispatch any functional type into the dispatcher. I want to have some function dispatch that can transfer a function into the Glib main loop: template
Philipp H.
  • 1,513
  • 3
  • 17
  • 31
3
votes
1 answer

Is there a data race on packaged task arguments?

A thread is created via a packaged task and a std::vector is returned. #include #include #include #include std::vector func(int &arg) { std::vector v = {1,2,3,4}; arg = 10; return…
curiousguy12
  • 1,741
  • 1
  • 10
  • 15
3
votes
1 answer

Can a member function be used anywhere a free function can using std::function?

I have some code (courtesy of progschj on GitHub) which I have adapted to exemplify my question. MakeTask moves any function and its arguments into MakeTask which makes a packaged_task. The created task is executed whereupon its future is returned…
rtischer8277
  • 496
  • 6
  • 27
3
votes
1 answer

c++: Building async with packaged_task

I am attempting to implement async with a packaged_task. I am attempting this via a templated function, bsync. bsync takes 2 arguments: a function,f, and a parameter-pack, args, and returns a future, fut. The future is of the type returned by…
3
votes
1 answer

c++ future.get() is not blocking

According to what's written about future in "the c++ programming language 4th edition" §5.3.5.1 page 120: If the value isn’t there yet, our thread is blocked until it arrives. Meaning that get() is a blocking method. Later in §5.3.5.2 page 122…
dikson231
  • 201
  • 5
  • 10
2
votes
1 answer

Direct initialization != copy initialization when converting from different type?

I always thought direct initialization and copy initialization for types T that do not match the class type are absolutely equal. Yet I seem to be mistaken. The following code doesn't compile if I copy initialize (using = ) and only compiles when I…
glades
  • 3,778
  • 1
  • 12
  • 34
2
votes
1 answer

How to understand the `callable` in the right way?

As per the document, which says: A Callable type is a type for which the INVOKE operation (used by, e.g., std::function, std::bind, and std::thread::thread) is applicable. This operation may be performed explicitly using the library function…
John
  • 2,963
  • 11
  • 33
2
votes
1 answer

C++ template parameter pack automatically adds & to its parameters

Check the below code. #include template void do_something(F f, Args... args) { using return_type = typename std::result_of::type; // Why below gives an error? …
Jaebum
  • 1,397
  • 1
  • 13
  • 33
2
votes
1 answer

Why does std::package_task fails to be called on GCC

Following code is ok on Microsoft and Clang compilers but fails on GCC. It throws std::system_error with message -1. Is it know issue? #include int main() { std::packaged_task task([](){}); task(); } GCC Clang Visual C++
Viktor
  • 1,004
  • 1
  • 10
  • 16
2
votes
0 answers

packaged_task operator() get exception

#include #include #include int simple_task(int i) { return i*3; } int main() { std::packaged_task task(simple_task); auto fut = task.get_future(); try { task(10); // why ???? got…
xren
  • 1,381
  • 5
  • 14
  • 29