ThreadPool class

Thread pool for parallel task execution.

This class implements a thread pool that manages a fixed number of worker threads. Tasks are submitted to a thread-safe queue and executed by the next available worker.

The pool uses a producer-consumer pattern:

  • Tasks are enqueued by main threads
  • Worker threads consume and execute tasks from the queue
  • Results are returned via std::future objects

Thread safety:

  • Task queue is protected by mutex and condition variable
  • Multiple threads can safely enqueue tasks
  • Worker threads terminate gracefully on destruction

Constructors, destructors, conversion operators

ThreadPool(size_t)
Construct a thread pool with the specified number of workers.
~ThreadPool()
Destructor - stops the pool and joins all worker threads.

Public functions

template<class F, class... Args>
auto enqueue(F&& f, Args && ... args) -> std::future< typename std::invoke_result< F, Args... >::type > -> auto
Enqueue a new task for execution.

Function documentation

ThreadPool::ThreadPool(size_t)

Construct a thread pool with the specified number of workers.

Exceptions
std::runtime_error if thread creation fails

Constructor implementation - spawns worker threads.

Creates a new thread pool and spawns the specified number of worker threads. Each worker runs continuously, waiting for tasks to execute.

Example: ThreadPool pool(4); // Create 4 worker threads

Creates the specified number of worker threads. Each worker runs an infinite loop, waiting for tasks to become available. Workers are spawned immediately and begin waiting for work.

Thread lifecycle:

  1. Worker thread starts
  2. Acquires lock on queue_mutex
  3. Waits on condition variable for work or stop signal
  4. When awakened, either exits (if stopped) or processes task
  5. Repeats from step 2

ThreadPool::~ThreadPool()

Destructor - stops the pool and joins all worker threads.

Destructor - stops pool and joins all worker threads.

Signals all worker threads to stop, waits for them to complete their current tasks, and then joins all threads. This function blocks until all threads have terminated.

Performs graceful shutdown of the thread pool:

  1. Sets stop flag to signal workers to terminate
  2. Notifies all workers via condition variable
  3. Joins each worker thread (blocks until thread completes)

template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args && ... args) -> std::future< typename std::invoke_result< F, Args... >::type >

Enqueue a new task for execution.

Template parameters
F Callable type
Args Argument types
Parameters
f Callable to execute
args Arguments to pass to callable
Returns std::future containing the result of the task
Exceptions
std::runtime_error if called after the pool is stopped
std::runtime_error if pool has been stopped

Enqueue a new task to the pool.

Adds a new task to the pool's queue. The task will be executed by an available worker thread. This function is thread-safe and can be called from multiple threads.

Example:

auto future = pool.enqueue([](int a, int b) { return a + b; }, 1, 2);
int result = future.get();  // result = 3

Creates a packaged_task from the provided callable and arguments, then adds it to the task queue. The task will be executed by an available worker thread.

Thread safety:

  • Acquires queue_mutex to safely add task to queue
  • Notifies one waiting worker via condition variable