C++ Concurrency Sandbox
Loading...
Searching...
No Matches
AgingPriorityPool Class Reference

A priority-based thread pool that implements an Aging Algorithm. More...

Collaboration diagram for AgingPriorityPool:

Public Member Functions

 AgingPriorityPool (size_t threads)
 Constructs the pool and starts both workers and the aging monitor.
 
void enqueue (int priority, std::string name, std::function< void()> f)
 Enqueues a task with an initial priority.
 
 ~AgingPriorityPool ()
 Gracefully shuts down the monitor and worker threads.
 

Detailed Description

A priority-based thread pool that implements an Aging Algorithm.

  • This pool uses a std::vector as a binary heap and a dedicated monitor thread to "age" tasks, ensuring that low-priority tasks eventually gain enough priority to be executed even under high load.

Definition at line 50 of file AgingPriority.cpp.

Constructor & Destructor Documentation

◆ AgingPriorityPool()

AgingPriorityPool::AgingPriorityPool ( size_t  threads)
inline

Constructs the pool and starts both workers and the aging monitor.

Parameters
threadsNumber of worker threads to spawn.

Definition at line 131 of file AgingPriority.cpp.

131 : stop(false) {
132 for (size_t i = 0; i < threads; ++i) {
133 workers.emplace_back(&AgingPriorityPool::workerLoop, this);
134 }
135 monitor_thread = std::thread(&AgingPriorityPool::monitorLoop, this);
136 }

◆ ~AgingPriorityPool()

AgingPriorityPool::~AgingPriorityPool ( )
inline

Gracefully shuts down the monitor and worker threads.

Definition at line 163 of file AgingPriority.cpp.

163 {
164 stop = true;
165 cv.notify_all();
166 if (monitor_thread.joinable()) monitor_thread.join();
167 for (auto &w : workers) if (w.joinable()) w.join();
168 }

Member Function Documentation

◆ enqueue()

void AgingPriorityPool::enqueue ( int  priority,
std::string  name,
std::function< void()>  f 
)
inline

Enqueues a task with an initial priority.

Parameters
priorityInitial importance level.
nameName for identifying the task in logs.
fThe function to execute.

Definition at line 144 of file AgingPriority.cpp.

144 {
145 {
146 std::lock_guard<std::mutex> lock(queue_mtx);
147 AgedTask newTask;
148 newTask.original_priority = priority;
149 newTask.current_priority = priority;
150 newTask.arrival_time = system_clock::now();
151 newTask.func = std::move(f);
152 newTask.task_name = name;
153
154 task_heap.push_back(std::move(newTask));
155 std::push_heap(task_heap.begin(), task_heap.end());
156 }
157 cv.notify_one();
158 }
Represents a task whose priority can increase over time.
int original_priority
The priority assigned at submission.
system_clock::time_point arrival_time
Timestamp of when the task entered the pool.
int current_priority
The boosted priority after aging.
std::function< void()> func
The callable task logic.
std::string task_name
Human-readable name for logging.

References AgedTask::arrival_time, AgedTask::current_priority, AgedTask::func, AgedTask::original_priority, and AgedTask::task_name.

Referenced by main().

Here is the caller graph for this function:

The documentation for this class was generated from the following file: