Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
thread_pool.H File Reference

A modern, efficient thread pool for parallel task execution. More...

#include <future>
#include <queue>
#include <functional>
#include <condition_variable>
#include <atomic>
#include <vector>
#include <thread>
#include <mutex>
#include <stdexcept>
#include <type_traits>
#include <tuple>
#include <memory>
#include <limits>
#include <optional>
#include <chrono>
#include <numeric>
#include <iterator>
Include dependency graph for thread_pool.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::queue_overflow_error
 Exception thrown when the task queue exceeds its hard limit. More...
 
struct  Aleph::ThreadPoolStats
 Statistics collected by ThreadPool. More...
 
class  Aleph::ThreadPool
 A reusable thread pool for efficient parallel task execution. More...
 
struct  Aleph::ThreadPool::TaskBase
 Type-erased task wrapper. More...
 
struct  Aleph::ThreadPool::Task< F >
 Concrete task implementation with type preservation. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Typedefs

using Aleph::ExceptionCallback = std::function< void(std::exception_ptr)>
 Type for exception callback in detached tasks.
 

Functions

template<typename Iterator , typename F >
void Aleph::parallel_for (ThreadPool &pool, Iterator begin, Iterator end, F &&f, size_t chunk_size=0)
 Execute a function in parallel over a range.
 
template<typename InputIt , typename OutputIt , typename F >
OutputIt Aleph::parallel_transform (ThreadPool &pool, InputIt first, InputIt last, OutputIt d_first, F &&f, size_t chunk_size=0)
 Transform elements in parallel and store results.
 
template<typename Iterator , typename T , typename BinaryOp >
T Aleph::parallel_reduce (ThreadPool &pool, Iterator first, Iterator last, T init, BinaryOp op, size_t chunk_size=0)
 Reduce elements in parallel.
 
template<typename F >
void Aleph::parallel_for_index (ThreadPool &pool, size_t start, size_t end, F &&f, size_t chunk_size=0)
 Apply a function to each element in parallel (index-based).
 
ThreadPoolAleph::default_pool ()
 Global default thread pool.
 

Detailed Description

A modern, efficient thread pool for parallel task execution.

This file provides a reusable thread pool that maintains a fixed number of worker threads, avoiding the overhead of creating new threads for each task (as happens with std::async).

Features

  • Zero thread creation overhead: Workers are pre-created and reused
  • Type-safe: Full template support with automatic return type deduction
  • Future-based: Returns std::future<T> for result retrieval
  • Exception-safe: Exceptions propagate through futures
  • Resizable: Can dynamically adjust the number of workers
  • Move-only support: Accepts move-only callables and arguments
  • Member functions: Direct support via std::invoke

Performance Comparison

Method Overhead per task
std::async 10-100 μs (thread creation)
ThreadPool ~50-100 ns (queue + notify)

Supported Callable Types

  • Lambdas (including move-only captures)
  • Free functions
  • Function pointers
  • Functors (function objects)
  • std::function
  • Member function pointers (with object)
  • std::bind expressions

Usage Examples

#include <thread_pool.H>
// Lambda with arguments
auto f1 = pool.enqueue([](int x, int y) { return x + y; }, 10, 20);
// Free function
auto f2 = pool.enqueue(std::sqrt, 16.0);
// Member function
struct Calculator {
int multiply(int a, int b) { return a * b; }
};
auto f3 = pool.enqueue(&Calculator::multiply, &calc, 6, 7);
// Move-only lambda
auto ptr = std::make_unique<int>(42);
auto f4 = pool.enqueue([p = std::move(ptr)]() { return *p; });
// Fire-and-forget (no future overhead)
pool.enqueue_detached([] { std::cout << "Background task\n"; });
std::cout << f1.get() << " " << f2.get() << " " << f3.get() << " " << f4.get();
// Output: 30 4 42 42
A reusable thread pool for efficient parallel task execution.
static mpfr_t y
Definition mpfr_mul_d.c:3
Basic arithmetic operations for the calculator.
A modern, efficient thread pool for parallel task execution.

Thread Safety

  • enqueue() and enqueue_detached() are thread-safe
  • resize() and shutdown() should not be called concurrently with each other
See also
worker_pool.H Alternative with different API
future_utils.H Utilities for working with futures
Author
Leandro Rabindranath León

Definition in file thread_pool.H.