Base Framework
Classes | Public Member Functions | List of all members
ThreadPool Class Reference

Thread pool maintainer. More...

#include <base/concurrency/ThreadPool.h>

Classes

class  ThreadPoolException
 Thread pool exception. More...
 

Public Member Functions

 ThreadPool (JobProvider *provider) noexcept
 
 ThreadPool (JobProvider *provider, unsigned int threads) noexcept
 
unsigned int getThreads () const noexcept
 
void setThreads (unsigned int value)
 
void terminate () noexcept
 
void join () noexcept
 
void post () noexcept
 
 ~ThreadPool () noexcept
 

Detailed Description

Thread pool maintainer.

Pool of threads used to run 'm' jobs using 'n' threads. The implementation is MT-safe.

Version
1.0

Constructor & Destructor Documentation

◆ ThreadPool() [1/2]

ThreadPool::ThreadPool ( JobProvider provider)
noexcept

Initializes thread pool with no threads.

Parameters
providerThe job provider.

◆ ThreadPool() [2/2]

ThreadPool::ThreadPool ( JobProvider provider,
unsigned int  threads 
)
noexcept

Initializes thread pool with specified number of threads.

Parameters
providerThe job provider.
threadsThe initial number of threads in the pool.

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )
noexcept

Destroys the thread pool.

Member Function Documentation

◆ getThreads()

unsigned int ThreadPool::getThreads ( ) const
noexcept

Returns the desired number of threads of the pool.

◆ join()

void ThreadPool::join ( )
noexcept

Waits for all the threads to terminate. All waiting jobs of the pool are executed first unless the pool has been explicitly terminated.

◆ post()

void ThreadPool::post ( )
noexcept

The job provider is required to invoke this function when a job becomes available.

◆ setThreads()

void ThreadPool::setThreads ( unsigned int  value)

Sets the desired number of threads of the pool. Blocks until accomplished. Raises ThreadPoolException is the pool has been terminated.

◆ terminate()

void ThreadPool::terminate ( )
noexcept

Asks all the threads to terminate. Jobs that have not been started are not executed.