CVR-Lib last update 20 Sep 2009

cvr::semaphore Class Reference

Class for inter-thread synchronisation concept of semaphores. More...

#include <cvrSemaphore.h>

List of all members.

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


Detailed Description

Class for inter-thread synchronisation concept of semaphores.

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.

See also:
cvr::mutex, cvr::thread

Constructor & Destructor Documentation

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).

Parameters:
initialValue initial value for the semaphore counter.

virtual cvr::semaphore::~semaphore (  )  [virtual]

Destructor.


Member Function Documentation

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 (  ) 

Post semaphore.

Post semaphore, i.e. increase its value

Returns:
true if successful, false otherwise

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.

Returns:
true if value was decreased.

bool cvr::semaphore::wait (  ) 

Wait on semaphore.

Wait on semaphore, i.e. decrease the value or wait if counter <= 0

Returns:
true if successful, false otherwise


Member Data Documentation

int cvr::semaphore::initValue [protected]

Initial semaphore value.

This value is required to allow resetting the semaphore

sem_t cvr::semaphore::theSemaphore [protected]

The posix semaphore.


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

Generated on Sun Sep 20 22:09:06 2009 for CVR-Lib by Doxygen 1.5.8