The RWReadersWriterLock Class
A readers-writer lock is a synchronization mechanism that combines mutual exclusion with condition synchronization to allow any number of readers or a single writer to acquire ownership of the lock. A readers-writer lock is useful in those situations where many readers exist for which mutual exclusion would unnecessarily reduce concurrency and where updates or writes occur infrequently when compared to reads.
The
RWReadersWriterLock class interface has semantics similar to that of a mutex, but the functions have been renamed for separate read and write access. A thread requests read access by calling the
acquireRead() method and requests write access by calling the
acquireWrite() method. To release either type of access, the thread calls the lock’s
release() function.
To avoid blocking during the acquisition of a readers-writer lock that is already owned by another thread or threads, use the tryAcquireRead() and tryAcquireWrite() methods. These functions return false if the lock is already exclusively owned by another thread and return true if the lock has been successfully acquired.
The Synchronization package implementation of the readers-write lock prefers writers over readers when granting access.