libcommonc++
0.7
|
A counting semaphore – a synchronization primitive that allows multiple processes to coordinate access to a shared resource. More...
#include <Semaphore.h++>
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... | |
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.
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.
name | The name of the semaphore. On POSIX systems, the name must consist of at most 14 alphanumeric characters and may not contain slashes. |
value | The initial value of the semaphore. Must be at least 1. |
perm | The permissions with which to create the semaphore, if it does not yet exist. |
~Semaphore | ( | ) |
Destructor.
Destroys the underlying semaphore object, if no other processes have a reference to it.
|
inline |
Get the name of the semaphore.
int getValue | ( | ) | const |
Get the current value of the semaphore.
void init | ( | ) |
Initialize the semaphore.
If the underlying semaphore object did not yet exist, it is created.
SystemException | If the operation fails. |
bool signal | ( | ) |
Signal the semaphore.
Increments the semaphore's value by 1. The semaphore must have previously been acquired.
bool tryWait | ( | ) |
Try to wait on the semaphore, returning immediately if it couldn't be acquired.
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.