cmake-template
Loading...
Searching...
No Matches
ThreadPool Class Reference

#include <threadpool.hpp>

Inheritance diagram for ThreadPool:
[legend]

Public Member Functions

 ThreadPool (size_t thread_count=std::thread::hardware_concurrency())
 Construct a new ThreadPool object. More...
 
 ~ThreadPool ()
 Destroy the ThreadPool object. More...
 
 ThreadPool (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
ThreadPooloperator= (ThreadPool &&)=delete
 
void shutdown () override
 Shuts down the thread pool and stops all worker threads. More...
 
void schedule (std::function< void()> task) override
 Enqueues a new task to be executed by the thread pool. More...
 
void wait () override
 Waits for all currently enqueued tasks to be completed. More...
 
- Public Member Functions inherited from IScheduler
virtual ~IScheduler ()=default
 
virtual void schedule (std::function< void()> task)=0
 Schedules a task to be executed by the scheduler. More...
 
virtual void wait ()=0
 Waits for all scheduled tasks to complete. More...
 
virtual void shutdown ()=0
 Shuts down the scheduler and stops all scheduled tasks. More...
 

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

ThreadPool::ThreadPool ( size_t  thread_count = std::thread::hardware_concurrency())

Construct a new ThreadPool object.

This constructor initializes the thread pool with a specified number of worker threads. The threads will be created and will start running immediately, waiting for tasks to be enqueued. The thread pool will manage the execution of tasks concurrently across the worker threads, allowing for efficient parallel processing of tasks.

Parameters
thread_countThe number of worker threads to create in the thread pool. By default, this is set to the number of hardware threads available on the system, allowing for efficient parallel execution of tasks

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )

Destroy the ThreadPool object.

This destructor is responsible for shutting down the thread pool and ensuring that all worker threads are properly joined before the ThreadPool object is destroyed. It will signal the worker threads to stop and then wait for them to finish executing any remaining tasks before cleaning up resources

◆ ThreadPool() [2/3]

ThreadPool::ThreadPool ( const ThreadPool )
delete

◆ ThreadPool() [3/3]

ThreadPool::ThreadPool ( ThreadPool &&  )
delete

Member Function Documentation

◆ operator=() [1/2]

ThreadPool & ThreadPool::operator= ( const ThreadPool )
delete

◆ operator=() [2/2]

ThreadPool & ThreadPool::operator= ( ThreadPool &&  )
delete

◆ schedule()

void ThreadPool::schedule ( std::function< void()>  task)
overridevirtual

Enqueues a new task to be executed by the thread pool.

This method allows you to add a new task to the thread pool's queue of tasks to be executed. The provided task will be added to the queue, and one of the worker threads will pick it up and execute it as soon as it is available. The task can be any callable object, such as a lambda, function pointer, or std::function

Parameters
taskThe task to be executed by the thread pool. This can be any callable object, such as a lambda, function pointer, or std::function

Implements IScheduler.

◆ shutdown()

void ThreadPool::shutdown ( )
overridevirtual

Shuts down the thread pool and stops all worker threads.

This method signals the worker threads to stop processing tasks and then waits for them to finish executing any remaining tasks before shutting down. After calling this method, the thread pool will no longer accept new tasks, and all worker threads will be joined before the method returns

Implements IScheduler.

◆ wait()

void ThreadPool::wait ( )
overridevirtual

Waits for all currently enqueued tasks to be completed.

This method blocks until all tasks that have been enqueued in the thread pool have been completed. It will wait for all worker threads to finish executing their current tasks and ensure that the task queue is empty before returning. This can be useful for synchronizing with the completion of tasks before proceeding with further operations

Implements IScheduler.


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