Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Threads Module Reference Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

RWSemaphore

Module:  Threads   Package:  Synchronization


RWSemaphore RWSynchObject

Local Index

Members

Non-Members

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;

Public Constructor

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

Public Destructor

~RWSemaphore();

Public Member Functions

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

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.
bool
tryAcquire();
void
V();

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.

Private Constructor

RWSemaphore(const RWSemaphore& second);

Private Member Operator

RWSemaphore&
operator=(const RWSemaphore& second);

See Also

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



Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.