he RWMutex has reader's and writer's
lock member functions. All reader locks obtain shared ownership. Any writer
lock obtains exclusive ownership. If the mutex is reader-locked and a writer
lock is requested then this particular request and all new requests (reader or
writer) block until all current reader locks are released. When these reader
locks are released then the exclusive writer lock is granted before any reader
locks are granted. When such writer lock is released then all other requests
race for ownership. If another writer gets it then others wait again. If a
reader gets it then all writers wait until most of the currently locked
readers go through.
The idea of implementation is very simple:
class RWMutex : boost::noncopyable
The following is the full list of public member functions:
Constructor, does not throw.
Locks the mutex for shared access, throws boost::thread_resource_error.
Unlocks the mutex from shared access, does not throw.
Locks the mutex for exclusive access, throws boost::thread_resource_error.
Unlocks the mutex from exclusive access, does not throw.
Upgrades shared access to exclusive access, may starve, throws
Downgrades to shared lock, does not throw.
Attempts to establish exclusive lock, returns true on success, throws
The functionality is similar to the boost::thread except for the described
above treatment of starvation. The ots::threading::RWMutex is encapsulated in
the metafunction ots::config::RWMutex. See the example of use in the section
Preventing race condition
). Note that the upgradeToWriterLock cannot have the same
functionality as writerLock because it would create unjustified deadlock
situations. It has single level locking and, thus, may starve. Also note, that
using writerLock and upgradeLock to lock the same resource may lead to
deadlock. This property is simply inherited from boost::thread.