last update 20 Sep 2009 |
#include <cvrSemaphore.h>
Public Member Functions | |
semaphore (const int initialValue=1) | |
virtual | ~semaphore () |
bool | wait () |
bool | tryWait () |
bool | post () |
int | getValue () |
void | reset () |
Protected Member Functions | |
void | destroy () |
Protected Attributes | |
int | initValue |
sem_t | theSemaphore |
The difference between semaphores and mutexes is that the later one only tracks if the mutex has been locked or not, while the semaphore counts the number of "locks" (called here "waits()") and requires the same number of unlocks (here "posts()") to release the access.
The semaphore is unlocked when the counter reaches the value zero (see getValue()), which means that in construction time you decide how far from unlocking the semaphore is (for values greater than zero) or if it is from the beginning unlocked (for value zero). Note that the default counter value is set to 1.
cvr::semaphore::semaphore | ( | const int | initialValue = 1 |
) |
Default constructor.
The semaphore is unlocked when the counter reaches the value zero, which means that in construction time you decide how far from unlocking the semaphore is (for values greater than zero) or if it is from the beginning unlocked (for value zero).
initialValue | initial value for the semaphore counter. |
virtual cvr::semaphore::~semaphore | ( | ) | [virtual] |
Destructor.
void cvr::semaphore::destroy | ( | ) | [protected] |
Destroy the semaphore.
int cvr::semaphore::getValue | ( | ) |
Get current value.
The returned value will be usually lower than zero or zero if the semaphore has been unlocked.
bool cvr::semaphore::post | ( | ) |
void cvr::semaphore::reset | ( | ) |
Reset value to initialValue.
bool cvr::semaphore::tryWait | ( | ) |
Try to wait on semaphore, but do not block.
If this function returns true, then you can get access to the resource you are trying to protect, but if it returns false, then you may not assume you have rights to do that.
bool cvr::semaphore::wait | ( | ) |
int cvr::semaphore::initValue [protected] |
sem_t cvr::semaphore::theSemaphore [protected] |
The posix semaphore.