Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query regarding read-write locks

I am delving on the java concurrent APIs and trying to understand the usefulness of read-write locks. The javadoc says that a readwrite block maintains a pair of locks, one for read and the other for write operations. While the write lock is exclusive access by a thread, multiple threads can acquire the read lock. So if in the read section all we are doing is read operations and we are anyways providing multiple threads access, what is the need to have the readlock in the first place ? Is there a scenario when the readwrite lock is actually useful?

like image 540
user496934 Avatar asked Aug 12 '13 07:08

user496934


People also ask

What is read and write lock in SQL?

It means that an exclusive lock can hold only one transaction on a resource at the same time. The user of this lock is known as a writer. This lock is imposed when the transaction wants to modify the page or row data.

What are read and write locks?

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.

How does MySQL read locks and write locks?

MySQL Locks: Read LocksIf the session holds the READ lock on a table, they cannot perform a write operation on it. It is because the READ lock can only read data from the table. All other sessions that do not acquire a READ lock are not able to write data into the table without releasing the READ 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.


2 Answers

.... what is the need to have the readlock in the first place

While reading, you need to prevent a writer acquiring the lock ... until all readers have finished. But it is OK for another reader to acquire the lock.

While writing, you need to prevent a reader acquiring the lock ... until the writer has finished.

(In other words, there can be one writer, OR multiple readers holding locks ... but not both.)

For the purposes of describing this, it is helpful to describe the behaviour as being two locks. What actually happens under the hood ... is implementation specific.


Is there a scenario when the readwrite lock is actually useful?

Well yea. In any scenario where you can distinguish between threads that require shared read-only access and those that require exclusive read-write (or write-only), a ReadWrite lock allows more concurrency than a simple Lock or a primitive mutex.

like image 110
Stephen C Avatar answered Sep 23 '22 12:09

Stephen C


If you're reading the data in many threads you're likely not to see the latest changes to the data due to visibility issues. There are many layers where data can be cached on per-thread basis: diffent layers of CPU cache, RAM access buffers and so on. Havling read lock in place you may be sure, that you're always observing the latest state.

Write locks are even stronger, providing an atomicity of access along with the visibility of the latest changes.

The main reason to have different kind of locks here is to have an ability to obtain sufficient level of synchronization without introducing too much overhead and locks for other threads.

Is there a scenario when the readwrite lock is actually useful?

It's highly useful when you have some in-memory data (Array, Collection or whatever), which is queried a lot by different threads, but the updates of this data happens quite rarely. In this case having separate locks (read lock for queries and write lock for updates) may give you a significant performace benefit.

like image 40
Jk1 Avatar answered Sep 22 '22 12:09

Jk1