Ocean
Ocean::ThreadPool Class Reference

This class implements a pool holding re-usable thread objects for individual use. More...

Inheritance diagram for Ocean::ThreadPool:

Data Structures

class  PoolThread
 This class implements one thread of this thread pool. More...
 

Public Types

using Function = std::function< void()>
 Definition of a caller function without return parameter. More...
 

Public Member Functions

 ThreadPool ()=default
 Creates a new thread pool object. More...
 
 ~ThreadPool () override
 Destructs a thread pool. More...
 
size_t capacity () const
 Returns the maximal number of thread objects allowed inside this pool. More...
 
size_t size () const
 Returns the number of currently busy/active threads in this pool. More...
 
bool setCapacity (const size_t capacity)
 Defines the maximal number of thread objects existing concurrently. More...
 
bool invoke (Function &&function)
 Invokes a function on one of the free threads of this pool. More...
 
size_t pending () const
 Returns the number of pending functions. More...
 
bool isEmpty () const
 Returns whether the ThreadPool has completed all jobs that have been submitted. More...
 

Protected Types

using UniquePoolThread = std::unique_ptr< PoolThread >
 Definition of a unique pointer holding a pool thread. More...
 
using PoolThreads = std::vector< UniquePoolThread >
 Definition of a vector holding pool threads. More...
 
using FunctionQueue = std::queue< Function >
 Definition of a queue holding functions. More...
 
- Protected Types inherited from Ocean::Thread
typedef std::pair< pthread_t, bool > TimedJoinPair
 Definition of a pair holding a thread id and a boolean state. More...
 
enum  ThreadPriority {
  PRIORITY_IDLE , PRIORTY_BELOW_NORMAL , PRIORTY_NORMAL , PRIORTY_ABOVE_NORMAL ,
  PRIORTY_HIGH , PRIORTY_REALTIME
}
 Definition of different thread priority values. More...
 

Protected Member Functions

void threadRun () override
 Thread run function. More...
 
- Protected Member Functions inherited from Ocean::Thread
 Thread (const Thread &thread)=delete
 Disabled copy constructor. More...
 
void createThread ()
 Creates the thread itself. More...
 
void destroyThread ()
 Destroys the thread itself. More...
 
void stopThreadExplicitly (const unsigned int timeout=5000u)
 Tries to stop the thread gracefully. More...
 
Threadoperator= (const Thread &thread)=delete
 The disabled assign operator. More...
 
 Thread (const std::string &name=std::string())
 Creates a new thread object. More...
 
 Thread (const unsigned int randomNumberSeedValue, const std::string &name=std::string())
 Creates a new thread object. More...
 
virtual ~Thread ()
 Destructs a thread object. More...
 
bool startThread ()
 Starts the thread. More...
 
void stopThread ()
 Informs the thread to stop. More...
 
bool terminateThread ()
 Terminates the thread. More...
 
bool joinThread (const unsigned int timeout=(unsigned int)(-1))
 Waits until this thread has been stopped. More...
 
bool shouldThreadStop () const
 Returns whether this thread should stop. More...
 
bool isThreadInvokedToStart () const
 Returns whether this thread has been invoked to start immediately. More...
 
bool isThreadActive () const
 Returns whether this thread is active. More...
 

Protected Attributes

PoolThreads busyPoolThreads_
 The busy pool threads. More...
 
PoolThreads idlePoolThreads_
 The idle pool threads. More...
 
FunctionQueue pendingFunctions_
 The pending functions. More...
 
size_t capacity_ = 2
 Maximal pool capacity, with range [1, infinity) More...
 
size_t poolThreadIdCounter_ = 0
 The counter for pool thread ids. More...
 
Lock lock_
 Pool lock. More...
 

Additional Inherited Members

- Static Protected Member Functions inherited from Ocean::Thread
static void sleep (unsigned int ms)
 Sleeps the calling thread for a given time. More...
 
static void giveUp ()
 Gives up the remaining thread time. More...
 
static ThreadId currentThreadId ()
 Returns the thread id of the current (calling) thread. More...
 
static ThreadPriority threadPriority ()
 Returns the priority of the current thread. More...
 
static bool setThreadPriority (const ThreadPriority priority)
 Sets the priority of the current thread. More...
 
template<typename TObject , typename TExpectedValue >
static bool waitForValue (TObject &object, const TExpectedValue &expectedValue, const double timeout=-1.0)
 Waits until an object/variable has an expected value. More...
 
template<typename TObject , typename TExpectedValue >
static bool waitForValue (TObject &object, const TExpectedValue &expectedValue, TemporaryScopedLock &temporaryScopedLock, const double timeout=-1.0)
 Waits until an object/variable has an expected value. More...
 
static int pthread_timedjoin_np (pthread_t thread, void **retval, const struct timespec *abstime)
 Implements a thread join function with timeout value. More...
 

Detailed Description

This class implements a pool holding re-usable thread objects for individual use.

This class is not a singleton, use ThreadPoolSingleton in case a singleton is sufficient.

See also
ThreadPoolSingleton, Scheduler, TaskQueue.

Member Typedef Documentation

◆ Function

using Ocean::ThreadPool::Function = std::function<void()>

Definition of a caller function without return parameter.

◆ FunctionQueue

using Ocean::ThreadPool::FunctionQueue = std::queue<Function>
protected

Definition of a queue holding functions.

◆ PoolThreads

using Ocean::ThreadPool::PoolThreads = std::vector<UniquePoolThread>
protected

Definition of a vector holding pool threads.

◆ UniquePoolThread

using Ocean::ThreadPool::UniquePoolThread = std::unique_ptr<PoolThread>
protected

Definition of a unique pointer holding a pool thread.

Constructor & Destructor Documentation

◆ ThreadPool()

Ocean::ThreadPool::ThreadPool ( )
default

Creates a new thread pool object.

◆ ~ThreadPool()

Ocean::ThreadPool::~ThreadPool ( )
override

Destructs a thread pool.

Member Function Documentation

◆ capacity()

size_t Ocean::ThreadPool::capacity ( ) const
inline

Returns the maximal number of thread objects allowed inside this pool.

Returns
Maximal thread capacity, with range [1, infinity); 2 by default

◆ invoke()

bool Ocean::ThreadPool::invoke ( Function &&  function)

Invokes a function on one of the free threads of this pool.

Parameters
functionThe function that will be invoked by a free thread
Returns
True, if the function will be invoked by a free thread, otherwise no free thread was available

◆ isEmpty()

bool Ocean::ThreadPool::isEmpty ( ) const
inline

Returns whether the ThreadPool has completed all jobs that have been submitted.

Returns
True, if the results of size() and pending() are atomically both 0

◆ pending()

size_t Ocean::ThreadPool::pending ( ) const
inline

Returns the number of pending functions.

Returns
The number of pending functions which have not been executed yet as the pool is at capacity, with range [0, infinity)

◆ setCapacity()

bool Ocean::ThreadPool::setCapacity ( const size_t  capacity)

Defines the maximal number of thread objects existing concurrently.

Parameters
capacityMaximal number of thread objects to be allowed inside this pool, with range (1, infinity)
Returns
True, if succeeded

◆ size()

size_t Ocean::ThreadPool::size ( ) const
inline

Returns the number of currently busy/active threads in this pool.

Returns
Thread number, with range [0, infinity)

◆ threadRun()

void Ocean::ThreadPool::threadRun ( )
overrideprotectedvirtual

Thread run function.

Implements Ocean::Thread.

Field Documentation

◆ busyPoolThreads_

PoolThreads Ocean::ThreadPool::busyPoolThreads_
protected

The busy pool threads.

◆ capacity_

size_t Ocean::ThreadPool::capacity_ = 2
protected

Maximal pool capacity, with range [1, infinity)

◆ idlePoolThreads_

PoolThreads Ocean::ThreadPool::idlePoolThreads_
protected

The idle pool threads.

◆ lock_

Lock Ocean::ThreadPool::lock_
mutableprotected

Pool lock.

◆ pendingFunctions_

FunctionQueue Ocean::ThreadPool::pendingFunctions_
protected

The pending functions.

◆ poolThreadIdCounter_

size_t Ocean::ThreadPool::poolThreadIdCounter_ = 0
protected

The counter for pool thread ids.


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