libcommonc++  0.7
Semaphore Class Reference

A counting semaphore – a synchronization primitive that allows multiple processes to coordinate access to a shared resource. More...

#include <Semaphore.h++>

Inheritance diagram for Semaphore:
Collaboration diagram for Semaphore:

Public Member Functions

 Semaphore (const String &name, uint_t value=1, const Permissions &perm=Permissions::USER_READ_WRITE)
 Construct a new Semaphore with the given name, initial value, and permissions. More...
 
 ~Semaphore ()
 Destructor. More...
 
void init ()
 Initialize the semaphore. More...
 
bool wait ()
 Wait on the semaphore. More...
 
void lock ()
 Equivalent to wait(). More...
 
bool tryWait ()
 Try to wait on the semaphore, returning immediately if it couldn't be acquired. More...
 
bool signal ()
 Signal the semaphore. More...
 
void unlock ()
 Equivalent to signal(). More...
 
int getValue () const
 Get the current value of the semaphore. More...
 
String getName () const
 Get the name of the semaphore. More...
 

Detailed Description

A counting semaphore – a synchronization primitive that allows multiple processes to coordinate access to a shared resource.

A Semaphore has an initial value, which represents the quantity of some shared resource. When a process acquires the semaphore, this value is decremented, and when it releases the semaphore, the value is incremented.

A Semaphore must be initialized before it can be used, via a call to the init() method.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ Semaphore()

Semaphore ( const String name,
uint_t  value = 1,
const Permissions perm = Permissions::USER_READ_WRITE 
)

Construct a new Semaphore with the given name, initial value, and permissions.

Parameters
nameThe name of the semaphore. On POSIX systems, the name must consist of at most 14 alphanumeric characters and may not contain slashes.
valueThe initial value of the semaphore. Must be at least 1.
permThe permissions with which to create the semaphore, if it does not yet exist.

◆ ~Semaphore()

~Semaphore ( )

Destructor.

Destroys the underlying semaphore object, if no other processes have a reference to it.

Member Function Documentation

◆ getName()

String getName ( ) const
inline

Get the name of the semaphore.

◆ getValue()

int getValue ( ) const

Get the current value of the semaphore.

◆ init()

void init ( )

Initialize the semaphore.

If the underlying semaphore object did not yet exist, it is created.

Exceptions
SystemExceptionIf the operation fails.

◆ lock()

void lock ( )
inlinevirtual

Equivalent to wait().

Implements Lock.

◆ signal()

bool signal ( )

Signal the semaphore.

Increments the semaphore's value by 1. The semaphore must have previously been acquired.

Returns
true if the semaphore was released, false otherwise.

◆ tryWait()

bool tryWait ( )

Try to wait on the semaphore, returning immediately if it couldn't be acquired.

Returns
true if the semaphore was acquired, false otherwise.

◆ unlock()

void unlock ( )
inlinevirtual

Equivalent to signal().

Implements Lock.

◆ wait()

bool wait ( )

Wait on the semaphore.

If the semaphore's value is greater than 0, it's value is decremented and the method returns. Otherwise, the method blocks until the value becomes greater than 0.

Returns
true if the semaphore was acquired, false if an error occurred.

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