Base Framework
Public Member Functions | Static Public Member Functions | List of all members
Semaphore Class Reference

Semaphore synchronization object. More...

#include <base/concurrency/Semaphore.h>

Inherits Resource.

Public Member Functions

 Semaphore (unsigned int value=0)
 
int getValue () const
 
void post ()
 
void wait () const
 
bool tryWait () const
 
 ~Semaphore ()
 
- Public Member Functions inherited from Resource
String getDescription () const
 
void setDescription (const String &description)
 
unsigned int getResourceId () const
 
unsigned int getCreatedById () const
 
AnyReference getHandle () const noexcept
 
template<class TYPE >
TYPE & getInternalHandle () const
 
 operator bool () const noexcept
 

Static Public Member Functions

static unsigned int getMaximum () noexcept
 

Additional Inherited Members

- Protected Attributes inherited from Resource
AnyReference handle
 

Detailed Description

Semaphore synchronization object.

Semaphore synchronization object. Semaphores are useful when resources need to be synchronized between multiple threads.

Version
1.2
Examples
testsuite/echod.cpp.

Constructor & Destructor Documentation

◆ Semaphore()

Semaphore::Semaphore ( unsigned int  value = 0)

Initializes the semaphore object. Raises OutOfDomain if the value exceeds the maximum value.

Parameters
valueThe initial value. The default is 0.

◆ ~Semaphore()

Semaphore::~Semaphore ( )

Destroys the semaphore object.

Member Function Documentation

◆ getMaximum()

static unsigned int Semaphore::getMaximum ( )
staticnoexcept

Returns the maximum value the semaphore may have. The maximum value is guaranteed to be greater than or equal to 32767 and less then or equal to Primitive<int>::MAXIMUM.

◆ getValue()

int Semaphore::getValue ( ) const

Returns the current value of the semaphore. Warning: this method may not be supported on the particular platform.

Returns
-1 if not supported.

◆ post()

void Semaphore::post ( )

Increments the semaphore and signals any thread waiting for a change. This method never blocks.

Examples
testsuite/echod.cpp.

◆ tryWait()

bool Semaphore::tryWait ( ) const

Non-blocking variant of wait.

Returns
True if the semaphore was decremented.

◆ wait()

void Semaphore::wait ( ) const

Decrements the semaphore and blocks if the semaphore is less than zero until another thread signals a change.

Examples
testsuite/echod.cpp.