Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

RWMutexLock


RWMutexLock RWSynchObject

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

Data Types

Member Functions

Package

Synchronization

Header File

#include <rw/sync/RWTMutexLock.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, a mutex semaphore represented by an HMTX handle under OS2, and a mutex represented by a HANDLE under Win32.

Example

Global Typedefs

typedef HMTX               RWMutexLockRep;     // OS/2 
typedef pthread_mutex_t    RWMutexLockRep;     // POSIX
typedef mutex_t            RWMutexLockRep;     // Solaris
typedef HANDLE             RWMutexLockRep;     // Win32

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;

Constructors

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

Destructor

~RWMutexLock(void);

Public Member Functions

void
acquire(void);
RWWaitStatus
acquire(unsigned long milliseconds);
void
acquireRead(void);
RWWaitStatus
acquireRead(unsigned long milliseconds);
void
acquireWrite(void);
RWWaitStatus
acquireWrite(unsigned long milliseconds);
RWMutexLockRep*
getMutexLockRep(void) const;
RWBoolean
isAcquired(void) const;

NOTE: Only available from the debug version of the library.
void
release(void);
RWBoolean
tryAcquire(void);
RWBoolean
tryAcquireRead(void);
RWBoolean
tryAcquireWrite(void);

See Also

RWNullMutexLock, RWTLockGuard<Resource>, RWTUnlockGuard<Resource>, RWTTryLockGuard<Resource>



Previous fileTop of DocumentContentsIndexNext file

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