YANE-Framework 1.1.0
Public Member Functions

yane::Utils::Semaphore Class Reference

Semaphore class. More...

List of all members.

Public Member Functions

virtual void acquire (int resources=1)
 Acquire resources.
virtual int available ()
 Return how many resources are available.
virtual void release (int resources=1)
 Release resources.
 Semaphore (int resources=0)
 Constructor.
virtual bool tryAcquire (int resources=1, unsigned int wait_msec=0)
 Acquire resources optionally without blocking the calling thread.

Detailed Description

Class to use and create semaphore objects. A semaphore is needed if two or more threads have to work with limited resources. Usually this class is used as following:


Constructor & Destructor Documentation

LIB_EXPORT yane::Utils::Semaphore::Semaphore ( int  resources = 0)

Create new semaphore object with available resources.

Parameters:
resourcesQuantity of resources
Exceptions:
threadExceptionThrown if thread creation failed

Member Function Documentation

LIB_EXPORT void yane::Utils::Semaphore::acquire ( int  resources = 1) [virtual]

Try to acquire resources. If resources > available, the calling thread is blocked until resources are available.

Parameters:
resourcesQuantity of resources to acquire
Exceptions:
threadExceptionThrown if acquire failed
valueExceptionThrown if resources are out of range
LIB_EXPORT int yane::Utils::Semaphore::available ( ) [virtual]

Return how many resources are available

Returns:
Quantity of available resources
LIB_EXPORT void yane::Utils::Semaphore::release ( int  resources = 1) [virtual]

Release resources

Parameters:
resourcesQuantity of resources to release
Exceptions:
threadExceptionThrown if release failed
valueExceptionThrown if resources are out of range
LIB_EXPORT bool yane::Utils::Semaphore::tryAcquire ( int  resources = 1,
unsigned int  wait_msec = 0 
) [virtual]

Try to acquire resources. If resources > available the thread will be blocked for some time. The function will return false unless the ressources could be acquired until then.

Parameters:
resourcesQuantity of resources to acquire
wait_msecIf resources aren't available the thread will be blocked for given milliseconds.
Returns:
True, if acquiring was successful
Exceptions:
valueExceptionThrown if resources are out of range