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
1 answer

is std::packaged_task really expensive?

I am surprised at the results of the following code using gcc 4.7.2 on Opensuse Linux: #include #include #include #include #include #include #include int main(void) { const long N =…
Klaas van Gend
  • 1,105
  • 8
  • 22
0
votes
1 answer

Associate packaged_task and thread in C++

all I have a class: class someClass { public: vector someOperation(int start, int end) { // do something... } } and a main function: int main() { someClass obj; std::packaged_task(int, int)>…
ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
-1
votes
1 answer

Why `std::thread()` and `std::packaged_task()` works different, although they both accept callable targets?

Here is a simple code snippet: #include #include #include void foo(int){} int main() { std::thread(foo, 1).join(); //works indeed std::packaged_task task{foo, 1}; //complian …
John
  • 2,963
  • 11
  • 33
-1
votes
1 answer

Error: cannot convert argument 1 from 'Packaged_Task::' to 'std::nullptr_t'

The following program seems to be an error associated with an explicit constructor. However, I'm unable to find that out. Using Visual Stduio 2017, the following error comes up on build: C:/data/msvc/14.28.29333/include\future(475): error C2664:…
1 2 3 4 5
6