Class ThreadPool¶
Defined in File high_level_energy.h
Class Documentation¶
-
class ThreadPool¶
High-performance thread pool for parallel file processing.
A thread-safe, efficient thread pool implementation designed specifically for high-throughput file processing tasks. Features dynamic work distribution, resource-aware task scheduling, and cluster-safe operation.
Features¶
Fixed-size thread pool to avoid creation/destruction overhead
Task queue with condition variable synchronization
Exception-safe task execution with error propagation
Graceful shutdown with proper thread joining
Memory-efficient task storage using std::function
Characteristics¶
Zero allocation after initialization for task submission
Lock-free task completion detection using atomics
Optimized for I/O-bound tasks (file reading/parsing)
Scales efficiently up to hardware thread limits
Public Functions
-
explicit ThreadPool(size_t num_threads)¶
Constructor with thread count specification.
Creates a fixed-size thread pool with the specified number of workers. Threads are created immediately and remain active until destruction.
Note
Recommended thread count is typically between hardware_concurrency/2 and hardware_concurrency for I/O-bound workloads on clusters
- Parameters:
num_threads – Number of worker threads to create
-
~ThreadPool()¶
Destructor with graceful shutdown.
Stops accepting new tasks, completes all pending tasks, and joins all worker threads. Ensures all resources are properly cleaned up.
-
template<typename F, typename ...Args>
inline auto enqueue(F &&f, Args&&... args) -> std::future<typename std::invoke_result<F, Args...>::type>¶ Submit a task for asynchronous execution.
Submits a task to the thread pool for asynchronous execution. The task will be executed by the next available worker thread. Returns a future that can be used to retrieve the result or wait for completion.
Note
This method is thread-safe and can be called concurrently
- Template Parameters:
F – Function type (automatically deduced)
Args – Argument types (automatically deduced)
- Parameters:
f – Function or callable to execute
args – Arguments to pass to the function
- Returns:
std::future<return_type> Future for task result
- Throws:
std::runtime_error – if pool is shutting down
-
inline size_t active_count() const¶
Get number of active (executing) tasks.
Returns the number of tasks currently being executed by worker threads. Useful for monitoring load and determining when processing is complete.
- Returns:
Current number of tasks being executed
-
void wait_for_completion()¶
Wait for all tasks to complete.
Blocks until all submitted tasks have completed execution. Does not prevent new tasks from being submitted during the wait.
-
inline bool is_shutting_down() const¶
Check if thread pool is shutting down.
- Returns:
true if shutdown has been initiated