SourcePro® API Reference Guide

Product Documentation:
   SourcePro
Documentation Home
List of all members | Public Types | Public Member Functions | Private Member Functions
RWReadersWriterLock Class Reference

A synchronization lock that allows concurrent access to multiple readers, but limits access to a single writer. More...

#include <rw/sync/RWReadersWriterLock.h>

Inheritance diagram for RWReadersWriterLock:
RWSynchObject

Public Types

typedef RWTLockGuard< RWReadersWriterLockLockGuard
 
typedef RWTReadLockGuard< RWReadersWriterLockReadLockGuard
 
typedef RWTReadUnlockGuard< RWReadersWriterLockReadUnlockGuard
 
typedef RWTTryLockGuard< RWReadersWriterLockTryLockGuard
 
typedef RWTTryReadLockGuard< RWReadersWriterLockTryReadLockGuard
 
typedef RWTTryWriteLockGuard< RWReadersWriterLockTryWriteLockGuard
 
typedef RWTUnlockGuard< RWReadersWriterLockUnlockGuard
 
typedef RWTWriteLockGuard< RWReadersWriterLockWriteLockGuard
 
typedef RWTWriteUnlockGuard< RWReadersWriterLockWriteUnlockGuard
 

Public Member Functions

 RWReadersWriterLock (RWCancellationState state=0)
 
 ~RWReadersWriterLock ()
 
void acquire ()
 
RWWaitStatus acquire (unsigned long)
 
void acquireRead ()
 
RWWaitStatus acquireRead (unsigned long)
 
void acquireWrite ()
 
RWWaitStatus acquireWrite (unsigned long)
 
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

 RWReadersWriterLock (const RWReadersWriterLock &)
 
RWReadersWriterLockoperator= (const RWReadersWriterLock &)
 

Additional Inherited Members

- Protected Member Functions inherited from RWSynchObject
 RWSynchObject (RWCancellationState state=0)
 
 RWSynchObject (RWStaticCtor)
 
 RWSynchObject (const RWSynchObject &second)
 
RWSynchObjectoperator= (const RWSynchObject &second)
 
void testCancellation ()
 

Detailed Description

RWReadersWriterLock is a multiple-readers, single-writer synchronization lock. With this type of lock, multiple readers are allowed concurrent access to the protected critical section. However, exclusive access is granted to writers: a reader and a writer cannot both enter the critical section, and only one writer at a time is granted access. A writer that attempts to acquire the lock blocks waiting for readers to exit the critical section. Note that because this lock favors writers over readers, the writer gets priority over any readers that try to access the lock after the time that the writer attempts to acquire it. A reader attempting to access the lock acquires it only after all writers attempting access have acquired and released the lock.

Example
#include <iostream>
#include <rw/sync/RWReadersWriterLock.h>
// Class Ratio maintains the ratio x : y. Once x and y
// are set in the constructor, any change to either x
// or y causes the other to change such that the
// ratio is maintained.
//
// It is important that modifications of x and y occur
// as an atomic operation since, during the brief period
// between modifying one and then the other, the object is
// in an inconsistent state.
class Ratio
{
private:
double x_;
double y_;
public:
Ratio(double x, double y) : x_(x), y_(y) { }
// The read locks in ratio() and getXandY()
// ensure that x_ and y_ can be accessed without
// worrying that the object is in a state where
// one has been modified without the other.
// However, because they are read locks, there is
// no limit to the number of threads that can
// be running in either of these two member functions
// simultaneously. Thus there is no unnecessary
// blocking when multiple threads wish only to read
// the data without modifying it.
double ratio() {
ReadGuard readlock(rdwrLock_);
return (double)x_ / (double)y_;
}
void getXandY(double& x, double& y) {
ReadGuard readlock(rdwrLock_);
x = x_;
y = y_;
}
// The write locks in setX() and setY() ensure that
// x_ and y_ are updated atomically so that the integrity
// of the ratio is always maintained. These methods
// obtain exclusive access to rdwrLock. All other
// threads will block upon trying to enter any of the
// four methods shown for this class.
void setX(int i) {
WriteGuard writelock(rdwrLock_);
double r = x_ / y_;
x_ = i;
y_ = x_ / r;
}
void setY(int i) {
WriteGuard writelock(rdwrLock_);
double r = x_ / y_;
y_ = i;
x_ = y_ * r;
}
};
See also
RWTReadLockGuard, RWTTryReadLockGuard, RWTReadUnlockGuard, RWTWriteLockGuard, RWTTryWriteLockGuard, RWTWriteUnlockGuard

Member Typedef Documentation

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.

Constructor & Destructor Documentation

RWReadersWriterLock::RWReadersWriterLock ( RWCancellationState  state = 0)

Creates and initializes an RWReadersWriterLock. The thread cancellation state of the object is initialized to state. Can throw RWTHRInternalError.

RWReadersWriterLock::~RWReadersWriterLock ( )

Recovers any system resources used to implement the RWReadersWriterLock. Can throw RWTHRInternalError.

RWReadersWriterLock::RWReadersWriterLock ( const RWReadersWriterLock )
private

Copy construction prohibited.

Member Function Documentation

void RWReadersWriterLock::acquire ( void  )
inline

Calls acquireWrite(). Provided for compatibility with simple mutex locks. Possible exceptions include RWCancellation and RWTHRInternalError.

RWWaitStatus RWReadersWriterLock::acquire ( unsigned long  milliseconds)
inline

Returns the result of calling acquireWrite(milliseconds). Provided for compatibility with simple mutex locks. Possible exceptions include RWCancellation and RWTHRInternalError.

void RWReadersWriterLock::acquireRead ( )

Acquires the lock as a reader. If there are writers waiting for the lock, the calling thread blocks until all readers currently in the protected code exit and until all waiting writers have acquired and left the protected code. At that time, the thread is given access to the protected code concurrently with other readers waiting on the lock.

This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.

This method returns immediately on success for single-threaded builds. A debug assertion occurs or an RWTHRInternalError is thrown on failure.

RWWaitStatus RWReadersWriterLock::acquireRead ( unsigned  long)

Attempts to acquire the lock as a reader. If there are writers waiting for the lock, the calling thread blocks until all readers currently in the protected code exit and until all waiting writers have acquired and left the protected code. At that time, the thread is given access to the protected code concurrently with other readers waiting on the lock.

If the lock cannot be acquired within the time specified, the function returns RW_THR_TIMEOUT.

This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.

This method returns immediately in single-threaded builds.

void RWReadersWriterLock::acquireWrite ( )

Acquires the lock as a writer. The calling thread is given priority access to the lock over all readers which have not yet acquired the lock. If readers already have acquired the lock, but have not released it, or another writer owns the lock, the call blocks until the lock is available. This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.

This method returns immediately on success for single-threaded builds. A debug assertion occurs or an RWTHRInternalError is thrown on failure.

RWWaitStatus RWReadersWriterLock::acquireWrite ( unsigned  long)

Attempts to acquire the lock as a writer. The calling thread isgiven priority access to the lock over all readers which have not yet acquired the lock. If readers already have acquired the lock, but have not released it, or another writer owns the lock, the call blocks until the lock is available. If the lock cannot be acquired within the time specified, the function returns RW_THR_TIMEOUT.

This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.

This method returns immediately in single-threaded builds.

RWReadersWriterLock& RWReadersWriterLock::operator= ( const RWReadersWriterLock )
private

Assignment prohibited.

void RWReadersWriterLock::release ( )

Releases the lock to one writer waiting to access the protected code. If there are no writers blocked waiting for the lock, all readers blocked on the lock are given concurrent access to the protected code. Possible exceptions include RWTHRInternalError.

bool RWReadersWriterLock::tryAcquire ( )
inline

Calls tryAcquireWrite(). Provided for compatibility with the interface used by simple mutex locks. Possible exceptions include RWCancellation and RWTHRInternalError.

bool RWReadersWriterLock::tryAcquireRead ( )

Attempts to acquire the lock as a reader. If there are no writers either waiting for or owning the lock, the calling thread acquires the lock, and this function returns true. Otherwise, this function returns false without acquiring the lock. Possible exceptions include RWCancellation and RWTHRInternalError.

bool RWReadersWriterLock::tryAcquireWrite ( )

Attempts to acquire the lock as a writer. If no thread currently owns the lock, the calling thread is given ownership of the lock and this function returns true. Otherwise, this function returns false without acquiring the lock. Possible exceptions include RWCancellation and RWTHRInternalError.

Copyright © 2021 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.