![]() |
VOOZH | about |
The std::packaged_task class wraps any Callable objects (function, lambda expression, bind expression, or another function object) so that they can be invoked asynchronously. A packaged_task won't start on its own, you have to invoke it, As its return value is stored in a shared state that can be called/accessed by std::future objects.
The main advantage of a packaged task is that it can link a callable object to a future and that is very important in a flooding environment. For example, if we have an existing function that fetches the data from Database (DB) and returns it. Now there is a need to execute this function in a separate thread. This can be done using:
std::packaged_task<>
Otherwise, we'll have to use:
std::promise<>
and have to change code but with the help of std::packaged_task<> its simple and we don't need to do that.
Some of the member functions in packaged_task are:
One of the non-member functions is:
Below is the C++ program to implement the above functions-
Output:
Result is = 720👁 packaged_task_output
720