C++ Concurrency Sandbox
Loading...
Searching...
No Matches
Priority.cpp File Reference
#include <iostream>
#include <vector>
#include <thread>
#include <functional>
#include <mutex>
#include <queue>
#include <condition_variable>
#include <atomic>
Include dependency graph for Priority.cpp:

Go to the source code of this file.

Classes

struct  PriorityTask
 A wrapper for a callable task associated with a priority level. More...
 
class  PriorityThreadPool
 A pool of worker threads that retrieves tasks based on priority. More...
 
struct  PaymentTask
 A Functor simulating a financial processing task. More...
 

Functions

int main ()
 Main function demonstrating priority-based out-of-order execution.
 

Function Documentation

◆ main()

int main ( void  )

Main function demonstrating priority-based out-of-order execution.

Definition at line 134 of file Priority.cpp.

134 {
135 // We use 1 thread to force tasks to wait in the queue, making
136 // the priority ordering visible in the console output.
137 PriorityThreadPool pool(1);
138
139 std::cout << "Submitting tasks in random order...\n";
140
141 // Submit Low Priority first
142 pool.enqueue(1, PaymentTask{"Low: Reward Statement"});
143 pool.enqueue(1, PaymentTask{"Low: SMS Notification"});
144
145 // Submit High Priority last
146 pool.enqueue(10, PaymentTask{"HIGH: FRAUD DETECTION"});
147 pool.enqueue(10, PaymentTask{"HIGH: AUTHORIZATION"});
148
149 // The output will show HIGH priority tasks running before the LOW priority
150 // tasks that were added earlier.
151
152 std::this_thread::sleep_for(std::chrono::seconds(2));
153 return 0;
154}
A pool of worker threads that retrieves tasks based on priority.
Definition Priority.cpp:45
A Functor simulating a financial processing task.
Definition Priority.cpp:119

References PriorityThreadPool::enqueue().

Here is the call graph for this function: