Main Page Namespace List Class Hierarchy Compound List Compound Members

# ZThread::Semaphore Class Reference

`#include <Semaphore.h>`

Inheritance diagram for ZThread::Semaphore::

List of all members.

## Public Methods
| Semaphore (int count=1, int maxCount=1) throw (Synchronization_Exception)
virtual | ~Semaphore () throw ()
void | wait () throw (Synchronization_Exception)
bool | tryWait (unsigned long) throw (Synchronization_Exception)
void | post () throw (Synchronization_Exception)
virtual int | count () throw ()
virtual bool | tryAcquire (unsigned long) throw (Synchronization_Exception)
virtual void | acquire () throw (Synchronization_Exception)
virtual void | release () throw (Synchronization_Exception)

## Detailed Description

Author:
  Eric Crahen

Version:
  1.3.4

Date:
  04-12-2001

A semaphore is an owner-less lock. It can be used to control a range or access to a set of elements. You create a Semaphore with an initial count and a maximum count. A semaphore can be acquired until its count drops to 0 - at which point it will block further attempts to acquire it. A semaphore is incremented until it hits the maximum, releasing a blocked thread each time its count is placed above 0.

A semaphore with a maximum of one is a binary semaphore and basically acts as a mutex. A mutex will be a little more efficient than a binary semaphore in most cases.

## Constructor & Destructor Documentation

Semaphore ( | int count = 1,
| int maxCount = 1 ) throw (Synchronization_Exception)

  | Create a new Semaphore. Parameters:
  count | - initial count
  maxCount | - upper bound
  Exceptions:
  Initialization_Exception | - resource could not be acquired
  Parameters:
  count |
  maxCount |

~Semaphore ( | ) throw ()`[virtual]`

| Destroy the Semaphore

## Member Function Documentation

void acquire ( | ) throw (Synchronization_Exception)`[virtual]`

  | Decrements the semaphore. If the count is 0 - block until its raised above 0. Exceptions:
  Interrupted_Exception | - thrown if the thread is interrupted while waiting for this semaphore, or if it was interrupted before the function was called
  Unexpected_Exception | - the calling thread was killed and can't be blocked
  Synchronization_Exception | - error with the underlying synchronization mechanisms
Reimplemented from ZThread::Lockable.

int count ( | ) throw ()`[virtual]`

| Current value of the semaphore Returns: int - count

void post ( | ) throw (Synchronization_Exception)

| See also: release() - provided to reflect the traditional Semaphore semantics

void release ( | ) throw (Synchronization_Exception)`[virtual]`

  | Increment the semaphore Exceptions:
  InvalidOp_Exception | - thrown if this would increase the count beyond the maximum
  Synchronization_Exception | - error with the underlying synchronization mechanisms
Reimplemented from ZThread::Lockable.

bool tryAcquire ( | unsigned long milliseconds ) throw (Synchronization_Exception)`[virtual]`

  | Decrements the semaphore. If the count is 0 - block until its raised above 0. Parameters:
  unsigned | long - amount of time to spend trying to acquire
  Returns: true - the semaphore was acquired in time, false if not Exceptions:
  Interrupted_Exception | - thrown if the thread is interrupted while waiting for this semaphore, or if it was interrupted before the function was called
  Unexpected_Exception | - the calling thread was killed and can't be blocked
  Synchronization_Exception | - error with the underlying synchronization mechanisms
  Parameters:
  milliseconds |
Reimplemented from ZThread::Lockable.

bool tryWait ( | unsigned long ) throw (Synchronization_Exception)

  | See also: tryAcquire() - provided to reflect the traditional Semaphore semantics Parameters:
  long |

void wait ( | ) throw (Synchronization_Exception)

| See also: acquire() - provided to reflect the traditional Semaphore semantics

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

- Semaphore.h