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
0
votes
0 answers

c++ std::packaged_task call throws system exception?

I've got a very simple code snippet from cpprefernce.com, as follows: #include #include using namespace std; int main(){ packaged_task t([](int i){return i+1;}); auto f = t.get_future(); cout<<"begin to call…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
0
votes
0 answers

Behavior of std::packaged_task and std::function

My question is from this implementation of a ThreadPool class in C++11. Following is relevant parts from the code: whenever enqueue is called on the threadPool object, it binds the passed function with all passed arguments, to create a shared_ptr…
aniliitb10
  • 1,259
  • 10
  • 14
0
votes
0 answers

detecting crashes of a std::packaged_task

I want to detect if a packaged task successfully completes, but have a few edge cases when errors in the task cause the thread to crash (from some 3rd party codes). In these cases no exceptions are caught by the below code and I am unable to tell…
Paul Childs
  • 229
  • 2
  • 9
0
votes
0 answers

Running a packaged_task after extracting it from a queue in mutual exclusion using a scoped_lock

I need to execute an extracted task from a queue after locking two mutex using a scoped_lock, the problem is to swap a task from a queue to another one and then execute it. So far this is my starting point std::packaged_task task; …
Antonio Santoro
  • 827
  • 1
  • 11
  • 29
0
votes
0 answers

Attempting to Queue up some variadic work

I am attempting to write a work queue, however I don't really want to know the argument types until later, anyways I am attempting to write something to the effect of std::queue> workQueue{}; template
0
votes
0 answers

Compilation error while passing queue of packaged_tasks as reference

Having a compilation error when I am trying to pass a queue containing packaged_task objects to a thread as a reference. I modified sample code to pass it to a function and the same compilation error observed void runtasks(std::queue<…
theh2o
  • 13
  • 4
0
votes
1 answer

Wrap and execute packaged_task inside lambda

I'm trying to wrap a packaged_task in a lambda in order to stock them inside a container. I wrote a test code below for simulating the wrapping and calling the lambda function. My code is as follows: int test() { return 10; } int main() { …
Minee
  • 408
  • 5
  • 12
0
votes
1 answer

Adding dummy copy constructor to std::packaged_task

I'm trying to bypass std::packaged_task's lack of a copy constructor so that I can pass it into a std::function (which will only ever be moved). I inherited from std::packaged_task and added a dummy copy constructor, which I assume shouldn't be…
0
votes
0 answers

C++ packaged_task hang in Concurrent Wrapper

I am implementing a concurrent wrapper as introduced by Herb Sutter presented in his talk "C++ and Beyond 2012". template class ConcurrentWrapper { private: std::deque>> _tasks; …
Jes
  • 2,614
  • 4
  • 25
  • 45
0
votes
1 answer

Using std::packaged_task to queue CAsyncSocket-Detach-socket tasks causes compile error when non-static Attach is called from static method

I am implementing code so I can Accept network connections as they arrive, Detach them from their arrival socket, create an std::packaged_task task, queue that task in a deque container, and then execute those tasks in their task thread later.…
rtischer8277
  • 496
  • 6
  • 27
0
votes
0 answers

C++ Future with Arguments Error

Below is the code I'm using to try and run a packaged_task. When I attempt to compile however, I receive the error: 'std::function<_Ret(CStore &, FileDetails &)>::function(std::function<_Ret(CStore &, FileDetails &)> &&)': cannot convert argument 1…
ImDevinC
  • 508
  • 1
  • 4
  • 18
0
votes
1 answer

c++: packaged_task via promises

I am attempting implement packaged_task as a template class, using promises. My compile error says that I am referencing a deleted function. I suspect I need to implement copy and/or move semantics, but I am confused how and where to begin. Any…
0
votes
1 answer

proper way of setting up a packaged_task

When compiling the program below, I get the error message: Error 1 error C2228: left of '.get_future' must have class/struct/union c:\users\haliaga\documents\visual studio 2010\projects\test\test\accumulateexceptionsafe.cpp 62 1 Test which…
0
votes
0 answers

C2248 error when using promise

The code below triggers the error: Error 1 error C2248: 'std::promise<_Ty>::promise' : cannot access private member declared in class 'std::promise<_Ty>' How can I fix it? Thanks a lot ! #define _parallel_qick_sort #ifdef…
user2286810
  • 136
  • 9
0
votes
1 answer

Use member function in std::packaged_task with std::thread (not std::async)

I'm trying to get a std::future from std::thread and it seems that the only way (?) to do so is to use std::packaged_task. Well, in addition, the function I want to call in std::thread is a class member function. I could find this thread Use member…
laobrasuca
  • 43
  • 6