A modern C++ thread pool implementation featuring priority-based task scheduling and dependency management.
-
Priority-based Task Scheduling
- Four priority levels: CRITICAL, HIGH, NORMAL, LOW
- Tasks with same priority execute in FIFO order
- Higher priority tasks preempt lower priority tasks
-
Task Dependencies
- Define task execution order using dependencies
- Automatic dependency resolution
- Cycle detection to prevent deadlocks
- Task unblocking when dependencies complete
-
Thread Management
- Configurable number of worker threads
- Automatic hardware concurrency detection
- Graceful and immediate shutdown options
- RAII-based resource management
-
Performance Monitoring
- Real-time worker status tracking
- Task completion statistics
- Exception tracking
- Queue utilization metrics
-
include/thread_pool.hpp- Main thread pool orchestrator
- Task submission interface
- Worker thread management
- Performance monitoring
-
include/task_queue.hpp- Thread-safe task queue
- Priority scheduling
- Dependency tracking
- Task state management
-
include/worker_thread.hpp- Individual worker thread
- Task execution
- Status reporting
- Exception handling
-
src/thread_pool.cpp- Thread pool implementation
- Task submission logic
- Worker lifecycle management
- Performance metrics
-
src/task_queue.cpp- Queue operations implementation
- Priority management
- Dependency resolution
- Thread synchronization
-
src/worker_thread.cpp- Worker thread implementation
- Task execution loop
- Status management
- Error handling
-
examples/basic_usage.cpp- Basic task submission
- Priority scheduling demo
- Worker status monitoring
-
examples/priority_dependency_demo.cpp- Priority scheduling examples
- Dependency chain demonstration
- Performance monitoring
ThreadPool pool(4); // Create pool with 4 workers
pool.start();
// Submit task with normal priority
pool.submit([]() {
std::cout << "Basic task" << std::endl;
});// Submit high priority task
pool.submit([]() {
std::cout << "Critical task" << std::endl;
}, TaskPriority::CRITICAL);
// Submit normal priority task
pool.submit([]() {
std::cout << "Normal task" << std::endl;
}, TaskPriority::NORMAL);// Task A (no dependencies)
size_t task_a = pool.submit_with_dependencies(
[]() { std::cout << "Task A" << std::endl; },
{}, // No dependencies
TaskPriority::NORMAL
);
// Task B (depends on A)
size_t task_b = pool.submit_with_dependencies(
[]() { std::cout << "Task B" << std::endl; },
{task_a}, // Depends on task A
TaskPriority::HIGH
);// Get worker status
auto status = pool.get_worker_status();
for (const auto& s : status) {
std::cout << s << std::endl;
}
// Get queue statistics
auto stats = pool.get_queue_stats();
std::cout << "Tasks in queue: " << stats.total_tasks << std::endl;
std::cout << "Completed tasks: " << stats.completed_tasks << std::endl;- C++17 or later
- CMake 3.10 or later
- pthread support
mkdir build
cd build
cmake ..
make./build/priority_dependency_demoThe thread pool provides thread-safe task submission and execution:
- Task queue operations are protected by mutex
- Worker status updates use atomic operations
- Task dependencies are managed safely
- Exception handling is thread-safe
- Uses priority queue for efficient task scheduling
- Minimizes lock contention in task queue
- Efficient dependency tracking with hash maps
- Cache-friendly data structures
- Non-blocking status monitoring
MIT License - See LICENSE file for details