Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MCSemaphore Class Reference

Representation of a named counting semaphore. More...

#include <mcllib/MCSemaphore.h>

Inheritance diagram for MCSemaphore:

MCMutex MCWaitNotify MCNonCopy List of all members.

Public Member Functions

virtual void lock ()
 Lock the semaphore object.
 MCSemaphore (const char *name, mcintn mode, mcintn initialValue=0, bool excl=false)
 Create or open a named semaphore.
 MCSemaphore (const char *name)
 Open a named semaphore.
virtual void notify ()
 Signal a single thread.
void post ()
 Post (signal) the semaphore.
virtual void unlock ()
 Unlock the semaphore object.
virtual void wait ()
 Wait for the semaphore to be signalled (posted).
 ~MCSemaphore ()
 Close the semaphore.

Static Public Member Functions

void remove (const char *name)
 Remove a named semaphore.

Detailed Description

Representation of a named counting semaphore.

Semaphores can be shared across processes. A name specified when the object is created is used to allow the IPC.


Constructor & Destructor Documentation

MCSemaphore const char *  name  ) 
 

Open a named semaphore.

The named semaphore must already exist. This means that some process must have called the alternate constructor which allows the semaphore to be created. If the named semaphore does not already exist an exception is thrown.

Parameters:
name the name of the semaphore to open

MCSemaphore const char *  name,
mcintn  mode,
mcintn  initialValue = 0,
bool  excl = false
 

Create or open a named semaphore.

If the named semaphore does not exist, it is created.

Parameters:
name the name of the semaphore to open or create
mode access mode of the semaphore if supported by the operating system. If the semaphore does not exist and needs to be created, mode specifies the desired access permissions of the created semaphore. If the semaphore already exists, mode is ignored.
initialValue the initial value of the semaphore
excl if true and the semaphore already exists then an exception is thrown

~MCSemaphore  ) 
 

Close the semaphore.

Note that the named semaphore will not be removed even if:-

  • this semaphore object was the one which created the semaphore
  • this is the last process to refer to the semaphore

The static remove() function must be called to remove the named semaphore object from the system.


Member Function Documentation

virtual void lock  )  [virtual]
 

Lock the semaphore object.

If another thread has locked the semaphore object and has not yet unlocked it, the calling thread blocks indefinitely until the other thread calls unlock() or notify(). This is the same as calling wait() on the semaphore.

Implements MCMutex.

virtual void notify  )  [virtual]
 

Signal a single thread.

This is the same as calling post() or unlock() on the semaphore.

Implements MCWaitNotify.

void post  ) 
 

Post (signal) the semaphore.

The value of the semaphore is incremented by one. This is the same as calling notify() or unlock() on the semaphore.

void remove const char *  name  )  [static]
 

Remove a named semaphore.

Parameters:
name the name of the semaphore to remove Note that care should be taken to ensure that named semaphores are removed from the system when the application is finished (or as part of application initialization). This is necessary because some platforms persist the semaphore name even after all references to it have gone away. (This is the POSIX semantics for instance).

virtual void unlock  )  [virtual]
 

Unlock the semaphore object.

If there are any threads blocked waiting for the semaphore to be unlocked, one of them will be woken up. This is the same as calling post() or notify() on the semaphore.

Implements MCMutex.

virtual void wait  )  [virtual]
 

Wait for the semaphore to be signalled (posted).

This is the same as calling lock() on the semaphore

Implements MCWaitNotify.


Generated on Wed Jan 12 19:05:50 2005 for MCLLIB by  doxygen 1.3.9.1