Ocean
Ocean::WorkerPool Class Reference

This class implements a pool holding worker objects for individual use. More...

Inheritance diagram for Ocean::WorkerPool:

Data Structures

class  ScopedWorker
 Definition of scoped worker object. More...
 

Public Member Functions

size_t capacity ()
 Returns the maximal number of worker objects allowed inside this pool. More...
 
size_t size ()
 Returns the number of currently existing worker objects in this pool. More...
 
bool setCapacity (const size_t workers)
 Defines the maximal number of worker objects existing concurrently. More...
 
ScopedWorker scopedWorker ()
 Returns a scoped object holding the real worker if available. More...
 
ScopedWorker conditionalScopedWorker (const bool condition)
 Returns a scoped object holding the real worker if a given condition is 'True' and if a worker is available. More...
 

Private Types

using UniqueWorker = std::unique_ptr< Worker >
 Definition of a unique pointer holding a Worker object. More...
 
using Workers = StaticVector< UniqueWorker, 10 >
 Definition of a static vector holding worker objects. More...
 

Private Member Functions

 WorkerPool ()=default
 Creates a new worker pool and initializes the maximal worker capacity to 2. More...
 
 ~WorkerPool ()
 Destructs a worker pool. More...
 
Workerlock ()
 Tries to lock a worker to be used for individual worker. More...
 
void unlock (Worker *worker)
 Unlocks a previously locked worker object to make it available for other users. More...
 

Private Attributes

Workers freeWorkers_
 Vector holding the currently not-used worker objects. More...
 
Workers usedWorkers_
 Vector holding the currently used worker objects. More...
 
size_t capacity_ = 2
 Maximal pool capacity, with range [1, infinity) More...
 
Lock lock_
 Lock for the entire pool. More...
 

Friends

class Singleton< WorkerPool >
 
class ScopedWorker
 

Additional Inherited Members

- Static Public Member Functions inherited from Ocean::Singleton< WorkerPool >
static WorkerPoolget ()
 Returns a reference to the unique object. More...
 
- Protected Member Functions inherited from Ocean::Singleton< WorkerPool >
 Singleton ()=default
 Default constructor. More...
 

Detailed Description

This class implements a pool holding worker objects for individual use.

See also
Worker.

Member Typedef Documentation

◆ UniqueWorker

using Ocean::WorkerPool::UniqueWorker = std::unique_ptr<Worker>
private

Definition of a unique pointer holding a Worker object.

◆ Workers

Definition of a static vector holding worker objects.

Constructor & Destructor Documentation

◆ WorkerPool()

Ocean::WorkerPool::WorkerPool ( )
privatedefault

Creates a new worker pool and initializes the maximal worker capacity to 2.

◆ ~WorkerPool()

Ocean::WorkerPool::~WorkerPool ( )
private

Destructs a worker pool.

Member Function Documentation

◆ capacity()

size_t Ocean::WorkerPool::capacity ( )
inline

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

Returns
Maximal worker capacity, with range [1, 10], 2 by default

◆ conditionalScopedWorker()

WorkerPool::ScopedWorker Ocean::WorkerPool::conditionalScopedWorker ( const bool  condition)
inline

Returns a scoped object holding the real worker if a given condition is 'True' and if a worker is available.

The scoped object guarantees the existence of the real worker (if provided in the moment this function is called) as long as the scoped object exists.
This function allows to apply something like:

// we invoke the following function in a single-core manner for small images (< 400 * 400 pixels),
// and in a multi-core manner for large images
invokeMulticoreFunction(frame, WorkerPool::get().conditionalScopedWorker(frame.pixels() >= 400u * 400u)());
static WorkerPool & get()
Returns a reference to the unique object.
Definition: Singleton.h:115
ScopedWorker conditionalScopedWorker(const bool condition)
Returns a scoped object holding the real worker if a given condition is 'True' and if a worker is ava...
Definition: WorkerPool.h:280
Parameters
conditionTrue, to get a scoped worker with real worker object (if available); False, to get an empty scoped worker
Returns
Object holding the worker if available, otherwise the object will be empty

◆ lock()

Worker* Ocean::WorkerPool::lock ( )
private

Tries to lock a worker to be used for individual worker.

Beware: This worker object must be unlocked after usage.

Returns
Worker object if available, otherwise nullptr
See also
unlock().

◆ scopedWorker()

ScopedWorker Ocean::WorkerPool::scopedWorker ( )

Returns a scoped object holding the real worker if available.

The scoped object guarantees the existence of the real worker (if available in the moment this function is called) as long as the scoped object exists.

Returns
Object holding the worker if available, otherwise the object will be empty

◆ setCapacity()

bool Ocean::WorkerPool::setCapacity ( const size_t  workers)

Defines the maximal number of worker objects existing concurrently.

Parameters
workersMaximal number of worker objects to be allowed inside this pool, with range [capacity(), 10]
Returns
True, if succeeded

◆ size()

size_t Ocean::WorkerPool::size ( )
inline

Returns the number of currently existing worker objects in this pool.

Returns
Worker count, with range [0, capacity()]

◆ unlock()

void Ocean::WorkerPool::unlock ( Worker worker)
private

Unlocks a previously locked worker object to make it available for other users.

Beware: To not use an unlocked worker object anymore.

Parameters
workerThe worker to be unlocked

Friends And Related Function Documentation

◆ ScopedWorker

friend class ScopedWorker
friend

◆ Singleton< WorkerPool >

friend class Singleton< WorkerPool >
friend

Field Documentation

◆ capacity_

size_t Ocean::WorkerPool::capacity_ = 2
private

Maximal pool capacity, with range [1, infinity)

◆ freeWorkers_

Workers Ocean::WorkerPool::freeWorkers_
private

Vector holding the currently not-used worker objects.

◆ lock_

Lock Ocean::WorkerPool::lock_
private

Lock for the entire pool.

◆ usedWorkers_

Workers Ocean::WorkerPool::usedWorkers_
private

Vector holding the currently used worker objects.


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