Skip to content

nk1408/thread-pool-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Thread Pool with Priority and Dependencies

A modern C++ thread pool implementation featuring priority-based task scheduling and dependency management.

Features

  • 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

Project Structure

Core Components

  • 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

Implementation Files

  • 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

  • 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

Usage Examples

Basic Task Submission

ThreadPool pool(4);  // Create pool with 4 workers
pool.start();

// Submit task with normal priority
pool.submit([]() { 
    std::cout << "Basic task" << std::endl; 
});

Priority-based Tasks

// 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 Dependencies

// 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
);

Status Monitoring

// 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;

Building

Requirements

  • C++17 or later
  • CMake 3.10 or later
  • pthread support

Build Commands

mkdir build
cd build
cmake ..
make

Running Examples

./build/priority_dependency_demo

Thread Safety

The 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

Performance Considerations

  • 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

License

MIT License - See LICENSE file for details

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors