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
1
vote
3 answers

C++11 packaged_task running with its own thread need a join() in order to get future

From the C++11 book I recently got, I am trying to build the example on page 123 (packaged_task 5.3.5.2) and after a few things to make this work with XCode, I have a couple of questions: First, there seems to be a requirement to pass a function…
John Difool
  • 5,572
  • 5
  • 45
  • 80
1
vote
0 answers

Runnable implementation using packaged_task in c++11

I am trying to create a Runnable interface in c++11 using packaged_task, with child class overriding run() function. I don't know why this code is not compiling. Its giving error related to type argument. /usr/include/c++/4.8.1/functional:1697:61:…
DeltaVega
  • 75
  • 1
  • 8
0
votes
0 answers

Adding std::bind placeholders to function arguments

I'm using a Merge class to encapsulate a merge sort. I'd like to use a packaged task to multithread the recursive calls: void Merge::executeSort() { packaged_task mergeLeft(bind(Merge::executeSort, _1)); …
Lucretiel
  • 3,145
  • 1
  • 24
  • 52
0
votes
1 answer

Can a only-movable object be made into a shared_ptr?

I am reading open source code of thread_pool And it shows // add new work item to the pool template auto ThreadPool::enqueue(F&& f, Args&&... args) -> std::future::type> { using…
f1msch
  • 509
  • 2
  • 12
0
votes
0 answers

error: no matching function for call to '__invoke_r'

The following simplified code introduces a function http_GET() that is based on a custom async implementation that takes a callback as parameter. The async function creates a (here temporary) object entity which allows a std::future to be exctracted…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

Why i am getting No associated state error in below example. Could anyone tell any reason?

In below program i am getting error std::future_error: No associated state error.could anyone help why i am facing this error #include #include #include #include #include void task(int& var,…
Mangy
  • 119
  • 6
0
votes
0 answers

How to add std::packaged_task to a member container through member function

I'd like to create a task scheduler and first thing I have to do is collect tasks in a container. This is what I'm trying #include #include #include using namespace std; template
danculo
  • 3
  • 2
0
votes
1 answer

C++11 packaged_task doesn't work as expected: thread quits and no output

I've this code snippet: #include #include using namespace std; int main() { cout << "---------" << endl; packaged_task task([](int a, int b){ cout << "task thread\n"; return a + b; }); thread…
Immanuel Kant
  • 517
  • 3
  • 8
0
votes
0 answers

Why the packaged task is terminating after throwing system error?

I am unsure why below code is throwing a runtime exception when run in online compiler here. I am trying to execute a function via packaged task #include #include int display(int i) { std::cout << "In display"; return i…
Helena
  • 444
  • 2
  • 15
0
votes
0 answers

Formal guarantee that empty packaged_task isn't optimized out?

Below is a simplified version of my scheduler. A single thread processes packaged_tasks in the order they were enqueued. execute schedules and waits for the completion of a task schedules enqueues a task and immediately returns to the user drain…
137
  • 183
  • 2
  • 14
0
votes
1 answer

Why is converting constructor of std::packaged_task explicit?

Why is the converting constructor of std::packaged_task explicit, while the same constructor of std::function is not? I cannot find any reasoning for it. This, for example, forces casting when passing a lambda as an argument for a function that has…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
0
votes
1 answer

Not all std::packaged_tasks executed when inside std::async calls

I have a rather complex code with std::async calls and std::packaged_task, that fails to execute to the very end. I simplified it to the minimal reproducable example. Two async functions are called one after another, inside of which there are…
rightaway717
  • 2,631
  • 3
  • 29
  • 43
0
votes
1 answer

Is std::packaged_task (Function Template) an std::async (FT) with an invoked function?

Trying to work with packaged_task std::async creates a thread executed asynchronously and process data which we can access into, using the class template std::future and the get() method. What should i know about how packaged_task works, in…
rekkalmd
  • 171
  • 1
  • 12
0
votes
1 answer

Creating a packaged task for a lambda function with iterators as arguments

(using C++17 and g++ 7.5.0 on Ubuntu 18.04) dp1 below works fine, but dp2 results in the following error: undefined reference to `pthread_create' collect2: error: ld returned 1 exit status #include #include #include…
Joe.S
  • 5
  • 2
0
votes
0 answers

i have error its meaning :Error C2440 'return': cannot convert from 'void (__cdecl &)(yield_context)' to 'void (&)(yield_context)'

I am making program in which i try to make boost packaged_task then take its future in vector and launch it with asio post. when i try to make packaged_task ,it gives me this error: Error C2440 'return': cannot convert from 'void (__cdecl…
ahmed allam
  • 377
  • 2
  • 15