Reputation: 50989
Please explain me more the contract. I can't figure out if two locks contained in ReentrantReadWriteLock
somehow related? Or these are just a bundle of two normal locks?
Upvotes: 6
Views: 3688
Reputation: 421
It allows multiple threads to read a resource concurrently, but requires a thread to wait for an exclusive lock in order to write to the resource.
Rules are:
Internally the lock state (c) is maintained by an int value. In this case, since we have read and write locks, it is logically divided into two shorts: The lower one representing the exclusive (writer) lock hold count, and the upper the shared (reader) hold count.
Assuming current state of lock is c= xxxx xxxx xxxx xxxx yyyy yyyy yyyy yyyy then Number of reader locks are the upper bits xxxx xxxx xxxx xxxx
Number of writer locks are the lower bits yyyy yyyy yyyy yyyy
Upvotes: 10
Reputation: 19185
If threads are waiting Read Lock it is shared but when thread wants to acquire write lock only that thread is allowed the access same as mutual exclusion.
So either one of operation is allowed .if lock is held by readers and thread request write lock no more readers are allowed to acquire read lock until thread which has acquired write lock release it
.
Upvotes: 2