SourcePro® 2022.1 |
SourcePro® API Reference Guide |
Implements a mutex, or mutual exclusion lock. More...
#include <rw/sync/RWMutexLock.h>
Public Types | |
typedef RWTLockGuard< RWMutexLock > | LockGuard |
typedef RWTReadLockGuard< RWMutexLock > | ReadLockGuard |
typedef RWTReadUnlockGuard< RWMutexLock > | ReadUnlockGuard |
typedef RWTTryLockGuard< RWMutexLock > | TryLockGuard |
typedef RWTTryReadLockGuard< RWMutexLock > | TryReadLockGuard |
typedef RWTTryWriteLockGuard< RWMutexLock > | TryWriteLockGuard |
typedef RWTUnlockGuard< RWMutexLock > | UnlockGuard |
typedef RWTWriteLockGuard< RWMutexLock > | WriteLockGuard |
typedef RWTWriteUnlockGuard< RWMutexLock > | WriteUnlockGuard |
Public Member Functions | |
RWMutexLock (RWStaticCtor) | |
RWMutexLock (RWCancellationState state=0) | |
~RWMutexLock () | |
void | acquire () |
RWWaitStatus | acquire (unsigned long milliseconds) |
void | acquireRead () |
RWWaitStatus | acquireRead (unsigned long milliseconds) |
void | acquireWrite () |
RWWaitStatus | acquireWrite (unsigned long milliseconds) |
RWMutexLockRep * | getMutexLockRep () const |
bool | isAcquired () const |
void | release () |
bool | tryAcquire () |
bool | tryAcquireRead () |
bool | tryAcquireWrite () |
Public Member Functions inherited from RWSynchObject | |
void | disableCancellation () |
void | enableCancellation (RWCancellationState) |
bool | isCancellationEnabled () const |
void | setCancellation (RWCancellationState) |
Private Member Functions | |
RWMutexLock (const RWMutexLock &second) | |
RWMutexLock & | operator= (const RWMutexLock &second) |
Related Functions | |
(Note that these are not member functions.) | |
typedef pthread_mutex_t | RWMutexLockRep |
typedef mutex_t | RWMutexLockRep |
typedef RW_MUTEX_T | RWMutexLockRep |
typedef void * | RWMutexLockRep |
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 () |
RWMutexLock implements a mutex or mutual exclusion lock. It can be used to permit only one thread at a time to access a section of code that is to be treated as a single atomic operation. Mutexes are typically used to protect sections of code that access shared resources or data. The sensitive area of code, or critical section, is bracketed by calls to acquire() and release() on the mutex.
All operations on the mutex, except initialization, are themselves guaranteed to be atomic in order to prevent race conditions from destroying the integrity of the lock. However, there is no reliable mechanism to guarantee that a race condition will not occur during initialization of a mutex lock. Generally, the application can ensure this by creating and initializing all mutexes in a primary thread of execution, before other threads are spawned. However, in the common case, when a mutex is used to protect static global data, there is no portable way to atomically initialize the mutex because C++ defines the order of initialization of static variables to be implementation-specific.
RWMutexLock provides some protection against race conditions during initialization of static global mutexes through a special constructor that does no initialization. When this constructor is used, initialization is postponed until the first attempt to acquire the lock. In order to prevent race conditions during mutex initialization by using this constructor, the application must still make sure the first acquire operation on such a mutex is not attempted concurrently by two or more threads.
This class is primarily a portable object wrapper for platform-specific mutex implementations. For example, RWMutexLock uses an instance of pthread_mutex_t
for POSIX conforming environments, mutex_t
on Solaris platforms, and a mutex represented by a HANDLE
under Win32.
If the Threads Module is built with the "Implement RWMutexLock with a
Critical Section" option enabled, the underlying implementation of the class is altered to use a CRITICAL_SECTION
under Win32 systems instead of a HANDLE
. This provides a significant performance boost in many cases at the cost of some portability and functionality.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
RWMutexLock::RWMutexLock | ( | RWStaticCtor | ) |
Creates an RWMutexLock but does no direct initialization. The mutex will be initialized upon first use.
The RWMutexLock static constructor must be used only for global static instances. Use of this constructor with an automatically or dynamically allocated instance produces errors or other unpredictable behavior.
The initialization of a global static mutex is not thread-safe. It is conceivable that two threads can attempt to acquire and initialize a mutex simultaneously, resulting in a race condition. When designing your application, take care to avoid such a possibility.
RWMutexLock::RWMutexLock | ( | RWCancellationState | state = 0 | ) |
Creates and initializes an RWMutexLock. The thread cancellation state of the object is initialized to state. Possible exceptions include RWTHRResourceLimit and RWTHRInternalError.
RWMutexLock::~RWMutexLock | ( | ) |
Recovers the system resource used to implement the RWMutexLock. Possible exceptions include RWTHRInternalError.
|
private |
Copy construction prohibited.
void RWMutexLock::acquire | ( | ) |
Causes the current thread to block until the mutex is released, at which time the thread acquires the mutex and continues. In the debug version of the Threads Module, this function will produce an assertion and abort if a thread attempts to recursively acquire the same mutex. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
This function throws an RWCancellation object if the mutex has cancellation detection enabled and a runnable containing the calling thread has been canceled.
In single-threaded builds, this method returns immediately. If the mutex was unavailable for acquisition, a debug assertion occurs or an RWTHRInternalError is thrown.
RWWaitStatus RWMutexLock::acquire | ( | unsigned long | milliseconds | ) |
Blocks at least for the specified number of milliseconds, or until the mutex is released, whichever comes first. If the mutex is released within the specified time, acquires it, and continues. If the mutex is not released, returns RW_THR_TIMEOUT. In the debug version of the Threads Module, this function produces an assertion and aborts if a thread attempts to recursively acquire the same mutex.
Note that true timed-acquisition is not supported on all platforms. For platforms without true timed-acquisition support, the function simply returns RW_THR_TIMEOUT if the mutex cannot be acquired immediately, Timed mutex acquisition is available if the macro RW_THR_HAS_TIMED_MUTEX_ACQUIRE
is defined.
This function throws an RWCancellation object if the mutex has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRResourceLimit and RWTHRInternalError.
This method returns immediately in single-threaded builds.
|
inline |
Calls acquire(). Provided for compatibility with read/write locks. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
inline |
Calls acquire(milliseconds). Provided for compatibility with read/write locks. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
inline |
Calls acquire(). Provided for compatibility with read/write locks. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
inline |
Calls acquire(milliseconds). Provided for compatibility with read/write locks. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
inline |
Provides access to the underlying platform-specific mutex implementation.
bool RWMutexLock::isAcquired | ( | ) | const |
Determines whether the calling thread currently owns the mutex.
|
private |
Assignment prohibited.
void RWMutexLock::release | ( | ) |
Releases the mutex, making it available. Possible exceptions include RWTHRInternalError.
bool RWMutexLock::tryAcquire | ( | ) |
Tries to acquire the mutex without blocking. Returns true
if the mutex is acquired, and false
if the mutex is not acquired.
Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
inline |
Calls tryAcquire(). Provided for compatibility with read/write locks. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
inline |
Calls tryAcquire(). Provided for compatibility with read/write locks. Possible exceptions include RWCancellation, RWTHRResourceLimit, and RWTHRInternalError.
|
related |
Typedef for the internal mutex lock.
|
related |
Typedef for the internal mutex lock.
|
related |
Typedef for the internal mutex lock.
RW_SYNC_MUTEX_USES_CRITICAL_SECTION
is defined.
|
related |
Typedef for the internal mutex lock.
RW_SYNC_MUTEX_USES_CRITICAL_SECTION
is not defined.
Copyright © 2022 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved. |