Packaged Task | Advanced C++ (Multithreading & Multiprocessing) Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 L lost1903 Follow Improve Article Tags : C++ cpp-multithreading Practice Tags : CPP Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like