Class DynamicThreadPool
Defined in File dynamic_threadpool.h
Class Documentation
-
class DynamicThreadPool
Class that implements a dynamically growing thread pool that can execute tasks in parallel.
Public Functions
-
DynamicThreadPool()
Create a new thread pool with unlimited maximum size.
-
explicit DynamicThreadPool(unsigned int max_size_)
Create a new thread pool with a maximum size.
Tasks that are posted to the threadpool may be queued if all threads are busy and the maximum size is reached. When threads become idle again, queued tasks will be executed. On shutdown, all queued tasks will still be executed before the threadpool is fully shut down.
- Parameters:
max_size_ – Maximum number of threads in the pool.
-
~DynamicThreadPool()
-
DynamicThreadPool(const DynamicThreadPool&) = delete
-
DynamicThreadPool &operator=(const DynamicThreadPool&) = delete
-
DynamicThreadPool(DynamicThreadPool&&) = default
-
DynamicThreadPool &operator=(DynamicThreadPool&&) = default
-
bool Post(const std::function<void()> &task_)
Posts a task to the thread pool for execution.
This method may either execute the task immediately in an idle thread if available, or create a new thread if the maximum size is not reached, yet. If the maximum size is reached and all threads are busy, the task will be queued
If shutdown has been called on the thread pool, posting a new task will fail.
- Parameters:
task_ – The task to be executed by the thread pool.
- Returns:
True if the task was successfully posted; otherwise, false.
-
size_t GetSize() const
Get the current number of threads in the pool.
- Returns:
The number of threads in the pool.
-
size_t GetIdleCount() const
Get the current number of idle threads in the pool.
- Returns:
The number of idle threads in the pool.
-
void Shutdown()
Shuts down the thread pool, allowing all queued tasks to finish.
After calling this method, no new tasks can be posted to the thread pool. This method does not block; to wait for all threads to finish, call Join() after Shutdown().
-
void Join()
Waits for all threads to finish execution.
This method must be called after Shutdown().
-
DynamicThreadPool()