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

RWMutexLock

Module:  Threads   Package:  Synchronization


RWMutexLock RWSynchObject

Local Index

Members

Non-Members

Header File

#include <rw/sync/RWMutexLock.h>

Description

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 user 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 by providing 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 user 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 will be 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.

Example

Global Typedefs

typedef bool              RWMutexLockRep;  // Single-Threaded
typedef pthread_mutex_t   RWMutexLockRep;  // POSIX
typedef mutex_t           RWMutexLockRep;  // Solaris
typedef HANDLE            RWMutexLockRep;  // Win32
typedef CRITICAL_SECTION  RWMutexLockRep;  // Win32
  // with Critical Section Implementation

Member Typedefs

typedef RWTLockGuard<RWMutexLock>          LockGuard;
typedef RWTReadLockGuard<RWMutexLock>      ReadLockGuard;
typedef RWTWriteLockGuard<RWMutexLock>     WriteLockGuard;

typedef RWTTryLockGuard<RWMutexLock>       TryLockGuard;
typedef RWTTryReadLockGuard<RWMutexLock>   TryReadLockGuard;
typedef RWTTryWriteLockGuard<RWMutexLock>  TryWriteLockGuard;

typedef RWTUnlockGuard<RWMutexLock>        UnlockGuard;
typedef RWTReadUnlockGuard<RWMutexLock>    ReadUnlockGuard;
typedef RWTWriteUnlockGuard<RWMutexLock>   WriteUnlockGuard;

Public Constructors

RWMutexLock(RWStaticCtor);
RWMutexLock(RWCancellationState
state=RW_CANCELLATION_DISABLED);

Public Destructor

~RWMutexLock();

Public Member Functions

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;

NOTE -- Only available from the debug version of the Threads Module.
void
release();
bool
tryAcquire();
bool
tryAcquireRead();
bool
tryAcquireWrite();

Private Constructor

RWMutexLock(const RWMutexLock& second);

Private Member Operator

RWMutexLock&
operator=(const RWMutexLock& second);

See Also

RWNullMutexLock, RWTLockGuard<Resource>, RWTUnlockGuard<Resource>, RWTTryLockGuard<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.