Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phRWLock Class Reference
[Threading and SynchronizationThread Safe]

#include <phRWLock.h>

Inheritance diagram for phRWLock:

phObject List of all members.

Detailed Description

This class provides a platform independent Reader-Writer class.

The phRWLock reader-writer lock class is a complete recusive lock system through any path that starts with the write lock being obtained. Should the read lock be the first lock obtained, then only read locks can be recursively obtained. Should a write lock be required and one attempts to obtain a write lock when the first lock is a read lock, then a dead lock will occur. However, if the write lock is obtained first, any combination of read and write locks may be performed. Remember that each lock obtained in a recursive lock system requires the same number of unlock calls.

Because of certain multiple inheritence issues in C++, the phRWLock class derives from phObject. Any class (phLiveObject) that wishes to implement a reader-writer lock through public methods needs to wrap all the reader-writer methods within that object and create a private instance to a phRWLock object. Since most Phission objects also inherit from phMutex, the unlock call conflicts with the phMutex::unlock call. For this reason, the reader-writer unlock is wrapped using the “rwUnlock” method to provide a distinction between the two. The phMutex lock/unlocks are used for protecting the object in these classes whereas the phRWLock lock/unlocks are used to provide concurrent reader access to data values while providing serial access to writers.

A potential area for problems is a continuous spinning on the reader-writer class methods that acquire and release the reader locked critical sections or writer locked critical sections. Since the algorithm for acquisition of the reader lock includes checking whether some reader already has the lock and just incrementing a value, there is potential to completely shut out a writer. It is not explicitly stated whether there is First In First Out / Queue functionality to track the order in which readers or writers try to lock. Reader-writer locks should be paired with a semaphore that can signal new information instead of looping on the locking methods for the reader-writer lock.

Implement a queue to track order of read/write lock attempts ???
Reader Writer algorithms for WIN32 (or anywhere for that matter):

I didn't use the above resources because I found the one below.

For WIN32, I used this reference:

Definition at line 188 of file phRWLock.h.

Public Member Functions

 phRWLock ()
 ~phRWLock ()
int readLock ()
int tryReadLock ()
int writeLock ()
int tryWriteLock ()
int rwUnlock ()

Private Member Functions

void init_lock ()

Private Attributes

phMutex m_mutex
ph_rwlock_info m_info
uint32_t m_readers
uintmax_t m_writer
uint32_t m_writer_nlocks

The documentation for this class was generated from the following files:

Copyright (C) 2002 - 2007 Philip D.S. Thoren ( )
University Of Massachusetts at Lowell
Robotics Lab Logo

Generated on Sat Jun 16 02:45:10 2007 for phission by  doxygen 1.4.4