ThreadPool.h file

Thread pool implementation for parallel task execution.

This module provides a thread pool implementation that enables parallel execution of tasks using a fixed number of worker threads. Tasks are submitted to a queue and executed by available workers.

Key features:

  • Fixed number of worker threads
  • Thread-safe task queue
  • std::future-based result retrieval
  • Automatic thread cleanup on destruction

Example usage:

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

// Submit a task and get a future
auto result = pool.enqueue([](int x) { return x * 2; }, 21);

// Wait for result
std::cout << result.get() << '\n';  // Outputs: 42

Performance characteristics:

  • Task submission: O(1) amortized
  • Task execution: Dependent on worker availability
  • Memory: O(threads) for worker threads + O(queued tasks)

Classes

class ThreadPool
Thread pool for parallel task execution.