|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Thread pool for parallel worker function execution. More...
#include <worker_pool.H>
Public Member Functions | |
| WorkersSet (WorkerFct worker_fct, const size_t qsize, const size_t n=16) | |
| Construct worker pool with specified thread count. | |
| void | shutdown () |
| Signal all workers to shut down. | |
| ~WorkersSet () | |
| Destructor shuts down and joins all worker threads, and frees the items that are still queued. | |
| void | prepare_num_workers (size_t n) |
| Set the expected number of work items. | |
| void | schedule_call (void *pars_ptr) |
| Schedule a work item for execution. | |
| bool | is_jobs_done () const |
| Check if a worker has signaled job completion. | |
| void | wait_until_all_workers_finished_or_job_is_done () |
| Block until all work items processed or job done signaled. | |
Private Member Functions | |
| void | worker_handler () |
Private Attributes | |
| WorkerFct | worker_fct = nullptr |
| size_t | num_threads = 0 |
| size_t | queue_capacity = 0 |
| std::mutex | m |
| std::condition_variable | cond |
| std::atomic< size_t > | num_workers = 0 |
| FixedQueue< void * > | q |
| DynList< std::thread > | threads |
| bool | shut_down = false |
| bool | job_done = false |
| std::condition_variable | job_done_cond |
Thread pool for parallel worker function execution.
Manages a pool of worker threads that execute a user-provided function. Work items are scheduled via a queue and distributed to available workers.
| WorkerFct | Function pointer type: bool (*)(void*) |
The worker function receives a void* to parameters and returns bool:
false: Continue processing more itemstrue: Signal "job done", stop all workersvoid* pointers and frees them with free(): they are allocated with malloc(), also those that the pool does not pass to the worker function, because a call already returned true or the pool shut down.Definition at line 119 of file worker_pool.H.
|
inline |
Construct worker pool with specified thread count.
| worker_fct | Function to execute for each work item |
| qsize | Maximum number of queued work items |
| n | Number of worker threads (default 16) |
Definition at line 208 of file worker_pool.H.
References Aleph::DynList< T >::append(), and WorkersSet< WorkerFct >::worker_handler().
|
inline |
Destructor shuts down and joins all worker threads, and frees the items that are still queued.
Definition at line 232 of file worker_pool.H.
References Aleph::FixedQueue< T >::get(), Aleph::FixedQueue< T >::is_empty(), and FunctionalMethods< Container, T >::mutable_for_each().
|
inline |
Check if a worker has signaled job completion.
Definition at line 282 of file worker_pool.H.
References m.
|
inline |
Set the expected number of work items.
| n | Number of work items to expect |
Definition at line 245 of file worker_pool.H.
References m.
|
inline |
Schedule a work item for execution.
The work item is added to the queue and will be picked up by an available worker thread.
| pars_ptr | Pointer to parameters (freed after worker returns) |
| std::overflow_error | if the queue already holds qsize items; the pool does not take pars_ptr then |
Definition at line 267 of file worker_pool.H.
References ah_overflow_error_if, m, Aleph::FixedQueue< T >::put(), and Aleph::FixedQueue< T >::size().
|
inline |
Signal all workers to shut down.
Workers will complete their current task and exit, and a wait for the items returns.
Definition at line 222 of file worker_pool.H.
References m.
|
inline |
Block until all work items processed or job done signaled.
Returns when either:
Definition at line 296 of file worker_pool.H.
References m.
|
inlineprivate |
Definition at line 141 of file worker_pool.H.
References Aleph::FixedQueue< T >::get(), Aleph::FixedQueue< T >::is_empty(), and m.
Referenced by WorkersSet< WorkerFct >::WorkersSet().
|
private |
Definition at line 128 of file worker_pool.H.
|
private |
Definition at line 135 of file worker_pool.H.
|
private |
Definition at line 139 of file worker_pool.H.
|
mutableprivate |
Definition at line 127 of file worker_pool.H.
|
private |
Definition at line 125 of file worker_pool.H.
|
private |
Definition at line 130 of file worker_pool.H.
|
private |
Definition at line 132 of file worker_pool.H.
|
private |
Definition at line 126 of file worker_pool.H.
|
private |
Definition at line 134 of file worker_pool.H.
|
private |
Definition at line 133 of file worker_pool.H.
|
private |
Definition at line 124 of file worker_pool.H.