Packaged Task | Advanced C++ (Multithreading & Multiprocessing) Last Updated : 05 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: 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. Member Functions 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. Non-Member Functions 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- C++ // C++ program to implement // the functions #include <bits/stdc++.h> using namespace std; // Factorial function int factorial(int N) { int res = 1; for (int i = N; i > 1; i--) { res *= i; } cout << "Result is = " << res << "\n"; return res; } // packaged task deque<packaged_task<int()> > task_q; mutex mu; condition_variable cond; void thread1() { // packaged task packaged_task<int()> t; { unique_lock<mutex> locker(mu); cond.wait(locker, []() { return !task_q.empty(); }); t = move(task_q.front()); task_q.pop_front(); } t(); } // Driver Code int main() { thread t1(thread1); // Create a packaged_task<> that // encapsulated the callback i.e. a function packaged_task<int()> t(bind(factorial, 6)); // Fetch the associated future<> // from packaged_task<> future<int> fu = t.get_future(); { lock_guard<mutex> locker(mu); task_q.push_back(move(t)); } cond.notify_one(); // Fetch the result of packaged_task<> cout << fu.get(); // Join the thread. Its blocking and // returns when thread is finished. t1.join(); return 0; } Output: Result is = 720720 Comment More infoAdvertise with us Next Article Socket Programming in C/C++: Handling multiple clients on server without multi threading L lost1903 Follow Improve Article Tags : C++ cpp-multithreading Practice Tags : CPP Similar Reads Multithreading in C++ Multithreading is a technique where a program is divided into smaller units of execution called threads. Each thread runs independently but shares resources like memory, allowing tasks to be performed simultaneously. This helps improve performance by utilizing multiple CPU cores efficiently. Multith 5 min read Multithreading in C A thread is a single sequence stream within a process. Because threads have some of the properties of processes, they are sometimes called lightweight processes. But unlike processes, threads are not independent from each other unlike processes. They share with other threads their code section, data 9 min read Multithreading in C A thread is a single sequence stream within a process. Because threads have some of the properties of processes, they are sometimes called lightweight processes. But unlike processes, threads are not independent from each other unlike processes. They share with other threads their code section, data 9 min read How to Get the List of All Running Tasks in C++? In C++, we have the one that may need the running tasks list for different purposes. In OS, we can get that list from the terminal using the tasklist system call. In this article, we will learn how to get the list of all the running processes using C++. Run Tasklist in C++ C++ provides the system() 2 min read Socket Programming in C/C++: Handling multiple clients on server without multi threading This tutorial assumes you have a basic knowledge of socket programming, i.e you are familiar with basic server and client model. In the basic model, server handles only one client at a time, which is a big assumption if you want to develop any scalable server model. The simple way to handle multiple 9 min read Advanced C++ Quizzes C++ provides many advanced features like preprocessors, multithreading, signal handling, and more. Understanding these concepts is helpful in writing high-performance code. This quiz will help you test your knowledge of advanced C++ topics.The below quizzes contain some questions each from the given 1 min read Like