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 | |
| ThreadPool & | operator= (const ThreadPool &)=delete |
| ThreadPool (ThreadPool &&)=delete | |
| ThreadPool & | operator= (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. | |
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
All public methods are thread-safe.
Non-copyable and non-movable.
| ThreadPool::ThreadPool | ( | int | threadCount = -1 | ) |
Constructs a ThreadPool with the given number of threads.
| threadCount | Number of worker threads. Defaults to std::thread::hardware_concurrency(). A value of 0 means tasks run inline on the calling thread. |
| int ThreadPool::activeThreadCount | ( | ) | const |
Returns the number of threads currently executing tasks.
| 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.
| count | The new thread count. |
Submits a callable for asynchronous execution.
If the thread count is 0, the callable is executed immediately on the calling thread.
| F | A callable type. |
| callable | The callable to execute. |
| int ThreadPool::threadCount | ( | ) | const |
Returns the current thread count.
Blocks until all submitted tasks have completed or the timeout expires.
| timeoutMs | Maximum time to wait in milliseconds. |