C12Adapter Opensource C++ Interface
MSemaphore Class Reference

Semaphore to be used to synchronize resource access. More...

Inheritance diagram for MSemaphore:

Public Member Functions

virtual ~MSemaphore ()
 Constructor that creates semaphore. More...
 
virtual void Unlock ()
 Unlock semaphore to allow other threads/processes to access the resource. More...
 
long UnlockWithCount (long count)
 Unlock semaphore with specific number of counts to allow other threads/processes to access the resource. More...
 
- Public Member Functions inherited from MSynchronizer
void Lock ()
 Lock without timeout. More...
 
bool LockWithTimeout (long timeout)
 

Additional Inherited Members

- Static Public Member Functions inherited from MSynchronizer
static void WaitForAll (MSynchronizer *p0, MSynchronizer *p1, MSynchronizer *p2=0, MSynchronizer *p3=NULL, MSynchronizer *p4=NULL)
 Wait until all the objects in the list are set to nonsignaled state. More...
 
static bool WaitWithTimeoutForAll (long timeout, MSynchronizer *p0, MSynchronizer *p1, MSynchronizer *p2=0, MSynchronizer *p3=NULL, MSynchronizer *p4=NULL)
 Wait until all the objects in the list are set to nonsignaled state, or timeout expires. More...
 
static unsigned WaitForAny (MSynchronizer *p0, MSynchronizer *p1, MSynchronizer *p2=NULL, MSynchronizer *p3=NULL, MSynchronizer *p4=NULL)
 Wait until any of the objects in the list are set to nonsignaled state. More...
 
static bool WaitWithTimeoutForAny (long timeout, unsigned *which, MSynchronizer *p0, MSynchronizer *p1, MSynchronizer *p2=0, MSynchronizer *p3=NULL, MSynchronizer *p4=NULL)
 Wait until any of the objects in the list are set to nonsignaled state, or timeout expires. More...
 
- Protected Member Functions inherited from MSynchronizer
 MSynchronizer ()
 Constructor that creates synchronizer. More...
 
virtual ~MSynchronizer ()
 Destructor.
 

Detailed Description

Semaphore to be used to synchronize resource access.

The implementation of the class is system dependent.

The semaphore is very often used with MSynchronizer::Lock helper class as the following:

MSemaphore semaphore;
...
void MyFunction()
{
MSemaphore::Locker locker(semaphore);
... // do any protected operation
// semaphore will be unlocked here automatically
}

Constructor & Destructor Documentation

virtual MSemaphore::~MSemaphore ( )
virtual

Constructor that creates semaphore.

Destructor.

Member Function Documentation

virtual void MSemaphore::Unlock ( )
virtual

Unlock semaphore to allow other threads/processes to access the resource.

This service unlocks the semaphore with the count of one. One needs to take into consideration the monitor-specific UnlockWithCount service too, which is also capable of returning a previous count.

Implements MSynchronizer.

long MSemaphore::UnlockWithCount ( long  count)

Unlock semaphore with specific number of counts to allow other threads/processes to access the resource.

This is a semaphore-specific variation of Unlock service. The function can be used when the number of the resources is increased to add the resource to control.

The function allows adding more than one resource and knowing the previous resource count. The return value is the previous semaphore count.