Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

RWSemaphore


RWSemaphore RWSynchObject

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Data Types

Member Functions

Package

Synchronization

Header File

#include <rw/sync/RWSemaphore.h> 

Description

An RWSemaphore is a synchronization object that maintains a non-negative counter. When a semaphore is acquired the count is decremented. If the count is zero when acquire() is called, then the calling thread will be put on a wait queue and block. When the thread reaches the head of the queue it can acquire the semaphore as soon as the counter is incremented. The count is incremented when a thread calls release() on the semaphore. The initial count of a semaphore is set when it is created; it defaults to 0.

An important difference between a mutex and a semaphore with a count of 1 is that a mutex can only be released by the thread that acquired it. A semaphore can be released by any thread.

Among the uses of semaphores are restricting access to some limited resource, and signaling some condition between threads.

Example

Global Typedef

typedef HANDLE     RWSemaphoreRep;      // Win32 Only

Member Typedefs

typedef RWTLockGuard<RWSemaphore>          LockGuard;
typedef RWTTryLockGuard<RWSemaphore>       TryLockGuard;
typedef RWTUnlockGuard<RWSemaphore>        UnlockGuard;

Constructor

RWSemaphore(unsigned count = 0, 
            RWCancellationState state=RW_CANCELLATION_DISABLED);

Destructor

~RWSemaphore(void);

Public Member Functions

void
acquire(void);
RWWaitStatus
acquire(unsigned long milliseconds);
RWSemaphoreRep*
getSemaphoreRep(void) const;
void
P(void);
RWWaitStatus
P(unsigned long milliseconds);
void
release(void);

NOTE: This method always increments the count, even if this increases the count beyond the count supplied to the constructor. For instance, the sequence sem = RWSemaphore(1); sem.release(); increases sem's count to 2.
RWBoolean
tryAcquire(void);
void
V(void);

NOTE: This method always increments the count, even if this increases the count beyond the count supplied to the constructor. For instance, the sequence sem = RWSemaphore(1); sem.v(); increases sem's count to 2.

See Also

RWSynchObject, RWTLockGuard<Resource>, RWTTryLockGuard, RWTTryLockGuard<Resource>, RWTUnlockGuard<Resource>



Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.