#include <semaphor.h>
Inheritance diagram for PSemaphore:
Public Member Functions | |
unsigned | GetInitial () const |
unsigned | GetMaxCount () const |
Construction | |
PSemaphore (unsigned initial, unsigned maximum) | |
PSemaphore (const PSemaphore &) | |
~PSemaphore () | |
Operations | |
virtual void | Wait () |
virtual PBoolean | Wait (const PTimeInterval &timeout) |
virtual void | Signal () |
virtual PBoolean | WillBlock () const |
Protected Member Functions | |
PQUEUE (ThreadQueue, PThread) | |
Protected Attributes | |
unsigned | initialVar |
unsigned | maxCountVar |
ThreadQueue | waitQueue |
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(); ...
PSemaphore::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.
initial | Initial value for semaphore count. |
maximum | Maximum value for semaphore count. |
PSemaphore::PSemaphore | ( | const PSemaphore & | ) |
Create a new semaphore with the same initial and maximum values as the original.
PSemaphore::~PSemaphore | ( | ) |
Destroy the semaphore. This will assert if there are still waiting threads on the semaphore.
unsigned PSemaphore::GetInitial | ( | ) | const [inline] |
unsigned PSemaphore::GetMaxCount | ( | ) | const [inline] |
PSemaphore::PQUEUE | ( | ThreadQueue | , | |
PThread | ||||
) | [protected] |
virtual void PSemaphore::Signal | ( | ) | [virtual] |
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.
Implements PSync.
Reimplemented in PSyncPointAck.
virtual PBoolean PSemaphore::Wait | ( | const PTimeInterval & | timeout | ) | [virtual] |
If the semaphore count is > 0, decrement the semaphore and return. If if is = 0 then wait (block) for the specified amount of time.
virtual void PSemaphore::Wait | ( | ) | [virtual] |
If the semaphore count is > 0, decrement the semaphore and return. If if is = 0 then wait (block).
Implements PSync.
virtual PBoolean PSemaphore::WillBlock | ( | ) | const [virtual] |
unsigned PSemaphore::initialVar [protected] |
unsigned PSemaphore::maxCountVar [protected] |
ThreadQueue PSemaphore::waitQueue [protected] |