Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documentation contradictions about ReentrantReadWriteLock. Does eventually write lock has priority over read lock or not in fair mode?

From ReentrantLock javadoc:

Fair mode
When constructed as fair, threads contend for entry using an approximately arrival-order policy. When the currently held lock is released either the longest-waiting single writer thread will be assigned the write lock, or if there is a group of reader threads waiting longer than all waiting writer threads, that group will be assigned the read lock.

A thread that tries to acquire a fair read lock (non-reentrantly) will block if either the write lock is held, or there is a waiting writer thread. The thread will not acquire the read lock until after the oldest currently waiting writer thread has acquired and released the write lock. Of course, if a waiting writer abandons its wait, leaving one or more reader threads as the longest waiters in the queue with the write lock free, then those readers will be assigned the read lock.

A thread that tries to acquire a fair write lock (non-reentrantly) will block unless both the read lock and write lock are free (which implies there are no waiting threads). (Note that the non-blocking ReentrantReadWriteLock.ReadLock.tryLock() and ReentrantReadWriteLock.WriteLock.tryLock() methods do not honor this fair setting and will acquire the lock if it is possible, regardless of waiting threads.)

Maybe it is problem with my English but I see contradictions at this decription:
From first paragrapgh I don't understand meaning of approximately arrival-order policy

  1. from first paragraph I understand that lock acquire the oldest waiting thread. If oldest thread - read thread then it will be group of read threads which wait longer than the longest waiting write thread.
  2. From second paragraph I understand that read lock will not acquire if write lock exists in wait-set.

Please clarify this contradiction.

like image 361
gstackoverflow Avatar asked Mar 15 '17 07:03

gstackoverflow


People also ask

What is the difference between reader/writer lock and normal 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.

What is the purpose of read lock?

Read lock: If there is no thread that has requested the write lock and the lock for writing, then multiple threads can lock the lock for reading. It means multiple threads can read the data at the very moment, as long as there's no thread to write the data or to update the data.

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.

What is the maximum number of threads that can possess the Writelock of a ReentrantReadWriteLock at the same time?

This lock supports a maximum of 65535 recursive write locks and 65535 read locks. Attempts to exceed these limits result in Error throws from locking methods.


1 Answers

Here, quoting from your quote:

or if there is a group of reader threads

In other words: a writer wins over a single reader; but when a group of readers wants the lock, those get the lock.

Regarding the question: "and what does group actually mean" ... that would be an implementation detail, only available by looking into the source code.

like image 78
GhostCat Avatar answered Sep 20 '22 12:09

GhostCat