libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ThreadPool Class Reference

General-purpose thread pool for submitting callable tasks. More...

#include <threadpool.h>

Public Member Functions

 ThreadPool (int threadCount=-1)
 Constructs a ThreadPool with the given number of threads.
 
 ~ThreadPool ()
 Destructor. Signals shutdown and joins all worker threads.
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&)=delete
 
ThreadPooloperator= (ThreadPool &&)=delete
 
template<typename F >
auto submit (F &&callable) -> Future< std::invoke_result_t< F > >
 Submits a callable for asynchronous execution.
 
void setThreadCount (int count)
 Resizes the thread pool.
 
int threadCount () const
 Returns the current thread count.
 
int activeThreadCount () const
 Returns the number of threads currently executing tasks.
 
void waitForDone ()
 Blocks until all submitted tasks have completed.
 
Error waitForDone (unsigned int timeoutMs)
 Blocks until all submitted tasks have completed or the timeout expires.
 
void clear ()
 Removes all pending (not yet running) tasks from the queue.
 

Detailed Description

General-purpose thread pool for submitting callable tasks.

Tasks are submitted via submit() which returns a Future for the result. The pool manages a set of worker threads that pull tasks from an internal

Example
ThreadPool pool(4); // 4 worker threads
Future<int> result = pool.submit([]() {
});
int value = result.get(); // blocks until ready
Dynamic array container wrapping std::vector.
Definition list.h:40
General-purpose thread pool for submitting callable tasks.
Definition threadpool.h:44
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
queue. When the thread count is set to 0, tasks run inline on the calling thread (useful for WASM graceful degradation).

All public methods are thread-safe.

Non-copyable and non-movable.

Constructor & Destructor Documentation

◆ ThreadPool()

ThreadPool::ThreadPool ( int  threadCount = -1)

Constructs a ThreadPool with the given number of threads.

Parameters
threadCountNumber of worker threads. Defaults to std::thread::hardware_concurrency(). A value of 0 means tasks run inline on the calling thread.

Member Function Documentation

◆ activeThreadCount()

int ThreadPool::activeThreadCount ( ) const

Returns the number of threads currently executing tasks.

Returns
The active thread count.

◆ setThreadCount()

void ThreadPool::setThreadCount ( int  count)

Resizes the thread pool.

If count is less than the current thread count, excess threads are stopped and joined. If greater, new threads are spawned. A count of 0 means subsequent tasks run inline.

Parameters
countThe new thread count.

◆ submit()

template<typename F >
auto ThreadPool::submit ( F &&  callable) -> Future<std::invoke_result_t<F>>
inline

Submits a callable for asynchronous execution.

If the thread count is 0, the callable is executed immediately on the calling thread.

Template Parameters
FA callable type.
Parameters
callableThe callable to execute.
Returns
A Future whose result type matches the callable's return type.

◆ threadCount()

int ThreadPool::threadCount ( ) const

Returns the current thread count.

Returns
The number of worker threads.

◆ waitForDone()

Error ThreadPool::waitForDone ( unsigned int  timeoutMs)

Blocks until all submitted tasks have completed or the timeout expires.

Parameters
timeoutMsMaximum time to wait in milliseconds.
Returns
Error::Ok if all tasks completed, Error::Timeout if the timeout elapsed.

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