Packaged Task | Advanced C++ (Multithreading & Multiprocessing)
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.
Need of packaged_task
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:
Otherwise, we’ll have to use:
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:
- Operator=- it moves packaged tasks and it’s a public member function.
- Swap- It just swaps to the packaged task or you can say exchange two packaged tasks with each other.
- get_future- It returns a std::future associated with the promised result.
- reset- This public member function just resets the task.
- (constructor)- As the name suggests this public member function constructs the packaged task.
- (destructor)- Similarly, (destructor) destructs the task object.
One of the non-member functions is:
- swap(packaged_task)- It specializes the std::swap algorithm.
Below is the C++ program to implement the above functions-