rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWReadersWriterLock Class Reference
[Synchronization]

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

List of all members.

Public Types

typedef RWTLockGuard
< RWReadersWriterLock
LockGuard
typedef RWTReadLockGuard
< RWReadersWriterLock
ReadLockGuard
typedef RWTWriteLockGuard
< RWReadersWriterLock
WriteLockGuard
typedef RWTTryLockGuard
< RWReadersWriterLock
TryLockGuard
typedef RWTTryReadLockGuard
< RWReadersWriterLock
TryReadLockGuard
typedef RWTTryWriteLockGuard
< RWReadersWriterLock
TryWriteLockGuard
typedef RWTUnlockGuard
< RWReadersWriterLock
UnlockGuard
typedef RWTReadUnlockGuard
< RWReadersWriterLock
ReadUnlockGuard
typedef RWTWriteUnlockGuard
< RWReadersWriterLock
WriteUnlockGuard

Public Member Functions

 RWReadersWriterLock (RWCancellationState state=0)
 ~RWReadersWriterLock ()
void acquire ()
void acquireRead ()
void acquireWrite ()
RWWaitStatus acquire (unsigned long)
RWWaitStatus acquireRead (unsigned long)
RWWaitStatus acquireWrite (unsigned long)
bool tryAcquire ()
bool tryAcquireRead ()
bool tryAcquireWrite ()
void release ()

Private Member Functions

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

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.

Examples

 #include <iostream.h>
 #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 cause 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:
   typedef RWReadersWriterLock::ReadLockGuard  ReadGuard
   typedef RWReadersWriterLock::WriteLockGuard WriteGuard
   double               x_;
   double               y_;
   RWReadersWriterLock  rdwrLock_;
 
 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<Resource> RWTTryReadLockGuard<Resource> RWTReadUnlockGuard<Resource> RWTWriteLockGuard<Resource> RWTTryWriteLockGuard<Resource> RWTWriteUnlockGuard<Resource>

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

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::acquire ( void   )  [inline]

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

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::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::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.

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.

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.

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© 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.