Logo Search packages:      
Sourcecode: pwlib version File versions  Download package

PSemaphore Class Reference

#include <semaphor.h>

Inheritance diagram for PSemaphore:

PObject PSyncPoint PSyncPointAck

List of all members.

Detailed Description

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: {verbatim} PSemaphore mutex(1, 1); // Maximum value of 1 and initial value of 1.



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


... {verbatim} 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.

Definition at line 151 of file semaphor.h.

Comparison functions

virtual Comparison Compare (const PObject &obj) const
virtual Comparison CompareObjectMemoryDirect (const PObject &obj) const
bool operator!= (const PObject &obj) const
bool operator< (const PObject &obj) const
bool operator<= (const PObject &obj) const
bool operator== (const PObject &obj) const
bool operator> (const PObject &obj) const
bool operator>= (const PObject &obj) const
enum  Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 }

Run Time Type functions

virtual const char * GetClass (unsigned=0) const
virtual BOOL InternalIsDescendant (const char *clsName) const
BOOL IsClass (const char *cls) const
static const char * Class ()

I/O functions

virtual void PrintOn (ostream &strm) const
virtual void ReadFrom (istream &strm)
ostream & operator<< (ostream &strm, const PObject &obj)
istream & operator>> (istream &strm, PObject &obj)

Public Member Functions

unsigned GetInitial () const
unsigned GetMaxCount () const
Miscellaneous functions
virtual PObjectClone () const
virtual PINDEX HashFunction () const
 PSemaphore (const PSemaphore &)
 PSemaphore (unsigned initial, unsigned maximum)
 ~PSemaphore ()
virtual void Signal ()
virtual BOOL Wait (const PTimeInterval &timeout)
virtual void Wait ()
virtual BOOL WillBlock () const

Protected Member Functions

 PQUEUE (ThreadQueue, PThread)

Protected Attributes

unsigned initialVar
unsigned maxCountVar
ThreadQueue waitQueue

Private Member Functions

PSemaphoreoperator= (const PSemaphore &)
 PCLASSINFO (PSemaphore, PSync)

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

Generated by  Doxygen 1.6.0   Back to index