SourcePro® 2023.1 |
SourcePro® API Reference Guide |
A synchronization object that maintains a non-negative counter. More...
#include <rw/sync/RWSemaphore.h>
Public Types | |
typedef RWTLockGuard< RWSemaphore > | LockGuard |
typedef RWTTryLockGuard< RWSemaphore > | TryLockGuard |
typedef RWTUnlockGuard< RWSemaphore > | UnlockGuard |
Public Member Functions | |
RWSemaphore (unsigned count=0, RWCancellationState state=0) | |
~RWSemaphore () | |
void | acquire () |
RWWaitStatus | acquire (unsigned long milliseconds) |
RWSemaphoreRep * | getSemaphoreRep () const |
void | P () |
RWWaitStatus | P (unsigned long milliseconds) |
void | release () |
bool | tryAcquire () |
void | V () |
Public Member Functions inherited from RWSynchObject | |
void | disableCancellation () |
void | enableCancellation (RWCancellationState) |
bool | isCancellationEnabled () const |
void | setCancellation (RWCancellationState) |
Private Member Functions | |
RWSemaphore (const RWSemaphore &second) | |
RWSemaphore & | operator= (const RWSemaphore &second) |
Related Functions | |
(Note that these are not member functions.) | |
typedef void * | RWSemaphoreRep |
Related Functions inherited from RWSynchObject | |
#define | RW_CANCELLATION_DISABLED |
#define | RW_CANCELLATION_ENABLED |
typedef void(* | RWCancellationState) () |
void | rwServiceCancellation (void) |
Additional Inherited Members | |
Protected Member Functions inherited from RWSynchObject | |
RWSynchObject (RWCancellationState state=0) | |
RWSynchObject (RWStaticCtor) | |
RWSynchObject (const RWSynchObject &second) | |
RWSynchObject & | operator= (const RWSynchObject &second) |
void | testCancellation () |
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 is put on a wait queue and blocks. 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 be released only 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.
OUTPUT:
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
RWSemaphore::RWSemaphore | ( | unsigned | count = 0 , |
RWCancellationState | state = 0 |
||
) |
Creates a semaphore with an initial counter set to count. The thread cancellation state of the object is initialized to state. Possible exceptions include RWTHRResourceLimit and RWTHRInternalError.
RWSemaphore::~RWSemaphore | ( | ) |
Recovers any system resources used to implement the RWSemaphore. Possible exceptions include RWTHRInternalError.
|
private |
Copy construction prohibited.
void RWSemaphore::acquire | ( | ) |
Acquires the semaphore by attempting to decrement the count. If the semaphore count is zero, the current thread blocks until another thread increments the count by calling release(). Otherwise, the count is decremented and the thread is allowed to proceed. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
This method returns immediately in single-threaded builds. If the resource is unavailable for acquisition, a debug assertion occurs or an RWTHRInternalError is thrown.
RWWaitStatus RWSemaphore::acquire | ( | unsigned long | milliseconds | ) |
Acquires the semaphore by attempting to decrement the count. If the semaphore count is zero, the current thread blocks until another thread increments the count or the specified amount of time expires. If the time has expired, the method returns RW_THR_TIMEOUT. Otherwise, the count is decremented and the thread is allowed to proceed. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
This method returns immediately in single-threaded builds.
RWSemaphoreRep* RWSemaphore::getSemaphoreRep | ( | ) | const |
Provides access to the underlying mechanism.
|
private |
Assignment prohibited.
|
inline |
Calls acquire(). P
stands for passeren, which is Dutch for to pass. Provided for those who wish to use this traditional sign handed down from the Dutch inventor of the semaphore, E. W. Dijkstra. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
inline |
Returns the result of calling acquire(milliseconds). P
stands for passeren, which is Dutch for to pass. Provided for those who wish to use this traditional sign handed down from the Dutch inventor of the semaphore, E. W. Dijkstra. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
void RWSemaphore::release | ( | ) |
Releases the semaphore by incrementing the semaphore count. Possible exceptions include RWTHRInternalError.
sem = RWSemaphore(1); sem.release();
increases the sempaphore's count to 2. bool RWSemaphore::tryAcquire | ( | ) |
Tries to acquire the semaphore by decrementing the count. If successful, returns true
; otherwise returns false
. Possible exceptions include RWCancellation and RWTHRInternalError.
|
inline |
Calls release(). V
stands for vrygeven, which is Dutch for "to release." Provided for those who wish to use this traditional sign handed down from the Dutch inventor of the semaphore, E. W. Dijkstra. Possible exceptions include RWTHRInternalError.
sem = RWSemaphore(1); sem.v();
increases the semaphore's count to 2.
|
related |
This is the internal semaphore representation.
Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved. |