Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are read and write locks in ReentrantReadWriteLock somehow related?

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?

like image 293
Dims Avatar asked Oct 26 '12 17:10

Dims


People also ask

What is the difference between read lock and write lock?

The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive. So you can have many readers at a time, but only one writer - and the writer will prevent readers from reading, too.

Which type of lock is obtained for both reading and writing?

With the Exclusive Lock, a data item can be read as well as written. Also called write lock.

How does read/write lock work?

ReadWriteLock is an advanced thread lock mechanism. It allows multiple threads to read a certain resource, but only one to write it, at a time. The idea is, that multiple threads can read from a shared resource without causing concurrency errors.

What are read/write locks in OS?

In many situations, data is read more often than it is modified or written. In these cases, you can allow threads to read concurrently while holding the lock and allow only one thread to hold the lock when data is modified. A multiple-reader single-writer lock (or read/write lock) does this.


2 Answers

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:

  • Several readers can share the resource concurrently. If you have a read lock, you can safely acquire another read lock. The maximum count of shared locks is 1111 1111 1111 1111
  • If you have a read lock, you CANNOT acquire a write lock
  • If you have a write lock, you CANNOT acquire a read lock in any other thread.
  • A reader can access the resource when there are no writers active in any other thread.
  • If you have a write lock, you can acquire another write lock in the same thread. The maximum count of exclusive locks that a thread can own is 1111 1111 1111 1111
  • A writer can access the resource when no other reader or writer (from a different thread) is active.
  • Prefers writers over readers. That is, if a writer is waiting on the lock, no new readers from other thread are allowed to access the resource. Existing readers can continue to use the resource until they release the lock. This prevents so-called "writer starvation".
  • Allows downgrading from the write lock to a read lock, by acquiring the write lock, then the read lock and then releasing the write lock. However, upgrading from a read lock to the write lock is not possible.

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

like image 99
Satish Avatar answered Sep 28 '22 00:09

Satish


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.

like image 40
Amit Deshpande Avatar answered Sep 28 '22 02:09

Amit Deshpande