This class implements a pool holding re-usable thread objects for individual use.
More...
#include <ThreadPool.h>
|
using | Function = std::function< void()> |
| Definition of a caller function without return parameter.
|
|
|
| ThreadPool ()=default |
| Creates a new thread pool object.
|
|
| ~ThreadPool () override |
| Destructs a thread pool.
|
|
size_t | capacity () const |
| Returns the maximal number of thread objects allowed inside this pool.
|
|
size_t | size () const |
| Returns the number of currently busy/active threads in this pool.
|
|
bool | setCapacity (const size_t capacity) |
| Defines the maximal number of thread objects existing concurrently.
|
|
bool | invoke (Function &&function) |
| Invokes a function on one of the free threads of this pool.
|
|
size_t | pending () const |
| Returns the number of pending functions.
|
|
bool | isEmpty () const |
| Returns whether the ThreadPool has completed all jobs that have been submitted.
|
|
|
void | threadRun () override |
| Thread run function.
|
|
| Thread (const Thread &thread)=delete |
| Disabled copy constructor.
|
|
void | createThread () |
| Creates the thread itself.
|
|
void | destroyThread () |
| Destroys the thread itself.
|
|
void | stopThreadExplicitly (const unsigned int timeout=5000u) |
| Tries to stop the thread gracefully.
|
|
Thread & | operator= (const Thread &thread)=delete |
| The disabled assign operator.
|
|
| Thread (const std::string &name=std::string()) |
| Creates a new thread object.
|
|
| Thread (const unsigned int randomNumberSeedValue, const std::string &name=std::string()) |
| Creates a new thread object.
|
|
virtual | ~Thread () |
| Destructs a thread object.
|
|
bool | startThread () |
| Starts the thread.
|
|
void | stopThread () |
| Informs the thread to stop.
|
|
bool | terminateThread () |
| Terminates the thread.
|
|
bool | joinThread (const unsigned int timeout=(unsigned int)(-1)) |
| Waits until this thread has been stopped.
|
|
bool | shouldThreadStop () const |
| Returns whether this thread should stop.
|
|
bool | isThreadInvokedToStart () const |
| Returns whether this thread has been invoked to start immediately.
|
|
bool | isThreadActive () const |
| Returns whether this thread is active.
|
|
|
static void | sleep (unsigned int ms) |
| Sleeps the calling thread for a given time.
|
|
static void | giveUp () |
| Gives up the remaining thread time.
|
|
static ThreadId | currentThreadId () |
| Returns the thread id of the current (calling) thread.
|
|
static ThreadPriority | threadPriority () |
| Returns the priority of the current thread.
|
|
static bool | setThreadPriority (const ThreadPriority priority) |
| Sets the priority of the current thread.
|
|
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.
|
|
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.
|
|
static int | pthread_timedjoin_np (pthread_t thread, void **retval, const struct timespec *abstime) |
| Implements a thread join function with timeout value.
|
|
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.
◆ Function
Definition of a caller function without return parameter.
◆ FunctionQueue
Definition of a queue holding functions.
◆ PoolThreads
Definition of a vector holding pool threads.
◆ UniquePoolThread
Definition of a unique pointer holding a pool thread.
◆ ThreadPool()
Ocean::ThreadPool::ThreadPool |
( |
| ) |
|
|
default |
Creates a new thread pool object.
◆ ~ThreadPool()
Ocean::ThreadPool::~ThreadPool |
( |
| ) |
|
|
override |
◆ 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
-
function | The 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
-
capacity | Maximal 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 |
◆ busyPoolThreads_
◆ capacity_
size_t Ocean::ThreadPool::capacity_ = 2 |
|
protected |
Maximal pool capacity, with range [1, infinity)
◆ idlePoolThreads_
◆ lock_
Lock Ocean::ThreadPool::lock_ |
|
mutableprotected |
◆ pendingFunctions_
◆ 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: