CDPThreadPool Class
The CDPThreadPool class is used to manage a pool of threads for executing tasks concurrently. More...
Header: | #include <Generic/CDPThreadPool.h> |
Public Functions
CDPThreadPool(std::size_t numThreads, std::size_t threadStackSize = 64 * 1024, CDP::ThreadPriority priority = CDP::LowThreadPriority, const std::string &threadBaseName = "CTPool") | |
~CDPThreadPool() | |
void | Await(double seconds) |
std::size_t | GetThreadCount() |
CDP::ThreadPriority | GetThreadPriority() |
std::size_t | GetThreadStackSize() |
bool | IsStopped() |
void | Join() |
void | Post(std::function<void() > task) |
void | Stop() |
Detailed Description
The CDPThreadPool class is used to manage a pool of threads for executing tasks concurrently.
Usage
- Instantiate an object of CDPThreadPool, specifying the number of threads, stack size per thread, thread priority, and a base name for threads.
- Use the Post() method to submit tasks to be executed by the thread pool.
- The Join(), Stop(), and Await() methods are available to synchronize with the execution of tasks.
CDPThreadPool pool(4); // Creates a pool with 4 threads pool.Post([](){ std::cout << "Task 1" << std::endl; }); pool.Post([](){ std::cout << "Task 2" << std::endl; }); pool.Join(); // Wait for all tasks to complete
See also CDP::ThreadPriority.
Member Function Documentation
CDPThreadPool::CDPThreadPool(std::size_t numThreads, std::size_t threadStackSize = 64 * 1024, CDP::ThreadPriority priority = CDP::LowThreadPriority, const std::string &threadBaseName = "CTPool")
Constructs a CDPThreadPool object with the specified number of threads, thread stack size, thread priority, and thread base name.
numThreads The number of threads in the pool. threadStackSize The stack size for each thread. priority The priority of threads. threadBaseName The base name for threads.
CDPThreadPool::~CDPThreadPool()
Destroys the CDPThreadPool object and ensures all threads have completed execution.
void CDPThreadPool::Await(double seconds)
Blocks the calling thread for the specified number of seconds or until all tasks have completed execution, whichever comes first.
seconds The number of seconds to wait.
std::size_t CDPThreadPool::GetThreadCount()
Returns the number of threads in the thread pool.
Returns the number of threads.
CDP::ThreadPriority CDPThreadPool::GetThreadPriority()
Returns the priority of threads in the thread pool.
Returns the thread priority.
std::size_t CDPThreadPool::GetThreadStackSize()
Returns the stack size of threads in the thread pool.
Returns the stack size.
bool CDPThreadPool::IsStopped()
Returns whether the thread pool has been stopped.
Returns true
if the thread pool has been stopped; otherwise false
.
void CDPThreadPool::Join()
Blocks the calling thread until all tasks have completed execution.
void CDPThreadPool::Post(std::function<void() > task)
Submits a task for execution by the thread pool.
task A function object representing the task to be executed.
void CDPThreadPool::Stop()
Stops the thread pool from executing any more tasks.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.