class PSemaphore

This class defines a thread synchonisation object.

Inheritance:


Public Methods

[more] Construction
[more] Operations


Inherited from PObject:

Public Methods

Run Time Type functions

I/O functions

Public Members

Comparison functions


Documentation

This class defines a thread synchonisation object. This is in the form of a integer semaphore. The semaphore has a count and a maximum value. The various combinations of count and usage of the Wait() and Signal() functions determine the type of synchronisation mechanism to be employed.

The Wait() operation is that if the semaphore count is > 0, decrement the semaphore and return. If it is = 0 then wait (block).

The Signal() operation is that if there are waiting threads then unblock the first one that was blocked. If no waiting threads and the count is less than the maximum then increment the semaphore.

The most common is to create a mutual exclusion zone. A mutex is where a piece of code or data cannot be accessed by more than one thread at a time. To prevent this the PSemaphore is used in the following manner:

      PSemaphore mutex(1, 1);  // Maximum value of 1 and initial value of 1.

      ...

      mutex.Wait();

      ... critical section - only one thread at a time here.

      mutex.Signal();

      ...
The first thread will pass through the Wait() function, a second thread will block on that function until the first calls the Signal() function, releasing the second thread.
o Construction

o PSemaphore( unsigned initial, unsigned maximum )
Create a new semaphore with maximum count and initial value specified. If the initial value is larger than the maximum value then is is set to the maximum value.
Parameters:
initial - Initial value for semaphore count.
maximum - Maximum value for semaphore count.

o PSemaphore(const PSemaphore &)
Create a new Semaphore with the same initial and maximum values as the original

o ~PSemaphore()
Destroy the semaphore. This will assert if there are still waiting threads on the semaphore.

o Operations

ovirtual void Wait()
If the semaphore count is > 0, decrement the semaphore and return. If if is = 0 then wait (block).

ovirtual BOOL Wait( const PTimeInterval & timeout )
If the semaphore count is > 0, decrement the semaphore and return. If if is = 0 then wait (block) for the specified amount of time.

Returns:
TRUE if semaphore was signalled, FALSE if timed out.

ovirtual void Signal()
If there are waiting (blocked) threads then unblock the first one that was blocked. If no waiting threads and the count is less than the maximum then increment the semaphore.

ovirtual BOOL WillBlock() const
Determine if the semaphore would block if the Wait() function were called.

Returns:
TRUE if semaphore will block when Wait() is called.


Direct child classes:
PSyncPoint
PMutex

Alphabetic index HTML hierarchy of classes or Java



This page was generated with the help of DOC++.