Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
WorkersSet< WorkerFct > Class Template Reference

Thread pool for parallel worker function execution. More...

#include <worker_pool.H>

Collaboration diagram for WorkersSet< WorkerFct >:
[legend]

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
 

Detailed Description

template<class WorkerFct>
class WorkersSet< WorkerFct >

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.

Template Parameters
WorkerFctFunction pointer type: bool (*)(void*)

Worker Function Contract

The worker function receives a void* to parameters and returns bool:

  • Return false: Continue processing more items
  • Return true: Signal "job done", stop all workers
Note
The pool takes ownership of scheduled void* 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.
Deprecated:
WorkersSet goes in 8.0.0: use ThreadPool (thread_pool.H).
Author
Leandro Rabindranath León

Definition at line 119 of file worker_pool.H.

Constructor & Destructor Documentation

◆ WorkersSet()

template<class WorkerFct >
WorkersSet< WorkerFct >::WorkersSet ( WorkerFct  worker_fct,
const size_t  qsize,
const size_t  n = 16 
)
inline

Construct worker pool with specified thread count.

Parameters
worker_fctFunction to execute for each work item
qsizeMaximum number of queued work items
nNumber of worker threads (default 16)

Definition at line 208 of file worker_pool.H.

References Aleph::DynList< T >::append(), and WorkersSet< WorkerFct >::worker_handler().

◆ ~WorkersSet()

template<class WorkerFct >
WorkersSet< WorkerFct >::~WorkersSet ( )
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().

Member Function Documentation

◆ is_jobs_done()

template<class WorkerFct >
bool WorkersSet< WorkerFct >::is_jobs_done ( ) const
inline

Check if a worker has signaled job completion.

Returns
true if worker returned true (job done)

Definition at line 282 of file worker_pool.H.

References m.

◆ prepare_num_workers()

template<class WorkerFct >
void WorkersSet< WorkerFct >::prepare_num_workers ( size_t  n)
inline

Set the expected number of work items.

Parameters
nNumber of work items to expect

Definition at line 245 of file worker_pool.H.

References m.

◆ schedule_call()

template<class WorkerFct >
void WorkersSet< WorkerFct >::schedule_call ( void *  pars_ptr)
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.

Parameters
pars_ptrPointer to parameters (freed after worker returns)
Exceptions
std::overflow_errorif the queue already holds qsize items; the pool does not take pars_ptr then
Note
Caller allocates pars_ptr with malloc(); pool takes ownership and frees it with free().

Definition at line 267 of file worker_pool.H.

References ah_overflow_error_if, m, Aleph::FixedQueue< T >::put(), and Aleph::FixedQueue< T >::size().

◆ shutdown()

template<class WorkerFct >
void WorkersSet< WorkerFct >::shutdown ( )
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.

◆ wait_until_all_workers_finished_or_job_is_done()

template<class WorkerFct >
void WorkersSet< WorkerFct >::wait_until_all_workers_finished_or_job_is_done ( )
inline

Block until all work items processed or job done signaled.

Returns when either:

  • All scheduled work items have been processed,
  • A worker function returned true (job done), or
  • The pool was shut down, whose workers leave the items

Definition at line 296 of file worker_pool.H.

References m.

◆ worker_handler()

template<class WorkerFct >
void WorkersSet< WorkerFct >::worker_handler ( )
inlineprivate

Member Data Documentation

◆ cond

template<class WorkerFct >
std::condition_variable WorkersSet< WorkerFct >::cond
private

Definition at line 128 of file worker_pool.H.

◆ job_done

template<class WorkerFct >
bool WorkersSet< WorkerFct >::job_done = false
private

Definition at line 135 of file worker_pool.H.

◆ job_done_cond

template<class WorkerFct >
std::condition_variable WorkersSet< WorkerFct >::job_done_cond
private

Definition at line 139 of file worker_pool.H.

◆ m

template<class WorkerFct >
std::mutex WorkersSet< WorkerFct >::m
mutableprivate

Definition at line 127 of file worker_pool.H.

◆ num_threads

template<class WorkerFct >
size_t WorkersSet< WorkerFct >::num_threads = 0
private

Definition at line 125 of file worker_pool.H.

◆ num_workers

template<class WorkerFct >
std::atomic<size_t> WorkersSet< WorkerFct >::num_workers = 0
private

Definition at line 130 of file worker_pool.H.

◆ q

template<class WorkerFct >
FixedQueue<void*> WorkersSet< WorkerFct >::q
private

Definition at line 132 of file worker_pool.H.

◆ queue_capacity

template<class WorkerFct >
size_t WorkersSet< WorkerFct >::queue_capacity = 0
private

Definition at line 126 of file worker_pool.H.

◆ shut_down

template<class WorkerFct >
bool WorkersSet< WorkerFct >::shut_down = false
private

Definition at line 134 of file worker_pool.H.

◆ threads

template<class WorkerFct >
DynList<std::thread> WorkersSet< WorkerFct >::threads
private

Definition at line 133 of file worker_pool.H.

◆ worker_fct

template<class WorkerFct >
WorkerFct WorkersSet< WorkerFct >::worker_fct = nullptr
private

Definition at line 124 of file worker_pool.H.


The documentation for this class was generated from the following file: