Questions tagged [readwritelock]

ReadWriteLock is a structure of the two locks. From these, one locks for reading (multiple threads can access, none can modify) and another for write access (only one thread can access and also modify at time).

In Java, ReadWriteLock is a very abstract interface which provides functionality to synchronize between multiple readers and one exclusive writer.

It just assumes some structure that contains two locks. Serious problems with this interface are unlikely. If you ask something about it, probably you mean ReentrantReadWriteLock, the implementation.

Related tags:

96 questions
80
votes
2 answers

When or why should I use a Mutex over an RwLock?

When I read the documentations of Mutex and RwLock, the difference I see is the following: Mutex can have only one reader or writer at a time, RwLock can have one writer or multiple readers at a time. When you put it that way, RwLock seems always…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
49
votes
8 answers

How to make a multiple-read/single-write lock from more basic synchronization primitives?

We have found that we have several spots in our code where concurrent reads of data protected by a mutex are rather common, while writes are rare. Our measurements seem to say that using a simple mutex seriously hinders the performance of the code…
sbi
  • 219,715
  • 46
  • 258
  • 445
48
votes
4 answers

How would you implement your own reader/writer lock in C++11?

I have a set of data structures I need to protect with a readers/writer lock. I am aware of boost::shared_lock, but I would like to have a custom implementation using std::mutex, std::condition_variable and/or std::atomic so that I can better…
jack
  • 2,094
  • 1
  • 19
  • 17
10
votes
3 answers

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…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
8
votes
2 answers

Do I ever *not* want to use a read/write lock instead of a vanilla mutex?

When synchronizing access to a shared resource, is there ever a reason not to use a read/write lock instead of a vanilla mutex (which is basically just a write lock), besides the philosophical reason of it having more features than I may need? In…
7
votes
2 answers

What strategy to use in Java for hierarchical reentrant read/write locking?

I'm looking for en efficient system to have a series of read/write locks organized hierarchically to manage access to hierarchically organized resources. If a subtree is locked for write, then no other lock should be able to be obtained in the whole…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
7
votes
3 answers

Mix volatile and synchronized as a read-write lock

Consider a primitive type variable with lots of threads reading and a few threads writing, will the following code work correctly? If it will, does it provide better performance than 1). declaring synchronized on all the methods; 2). using an…
Hongbo
  • 1,107
  • 2
  • 11
  • 18
7
votes
2 answers

Java : ReentrantReadWriteLock with priority

The following is the typical reader and writer pattern (a lot of reads and few writes) private ReadWriteLock lock = new ReentrantReadWriteLock(); private int value; public void writeValue(int newValue){ lock.writeLock().lock(); …
6
votes
2 answers

Best Practice to design tables to support update one field faster using sql server

I am working on Workflow like system. I have one task table and status field. Value for status can be one of New,ready,processing,error,abort,done. I have about 7 processes which will be triggered based on different situation to change value of task…
6
votes
1 answer

Synchronized vs ReadWriteLock performance

I try to prove that synchronized is slower when there are many readers and only some writers. Somehow I proved the opposite. The RW example, time of execution is 313 ms: package zad3readWriteLockPerformance; import java.util.ArrayList; import…
majewa
  • 83
  • 1
  • 5
6
votes
2 answers

QReadWriteLock recursion

I'm using QReadWriteLock in recursive mode. This code doesn't by itself make sense, but the issues I have arise from here: lock->lockForWrite(); lock->lockForRead(); lockForRead is blocked. Note that this is in recursive mode. The way i see it is…
0xbaadf00d
  • 2,535
  • 2
  • 24
  • 46
5
votes
1 answer

Implementing write-preferring R/W lock

I have a mutex library, and am trying to implement a write-preferring lock. I am looking at this example: https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock I understand the read-preferring lock, but I don't understand the write-preferring…
user7898461
5
votes
1 answer

Upgrading read lock to write lock without releasing the first in C++11?

I know it's possible using boost::UpgradeLockable in C++14. Is there anything similar for C++11?
SagiLow
  • 5,721
  • 9
  • 60
  • 115
5
votes
4 answers

Why to use a readlock?

I read that a write lock is exclusive and a read lock is shared , so a piece of code which in readlock anyway can be accessed by multiple threads . What if no read lock is acquired by the threads in contention . Any way they are going to read only…
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41
5
votes
1 answer

using java FileChannel FileLock to prevent file writes but allow reads

I think I'm misunderstanding how the FileChannel's locking features work. I want to have an exclusive write lock on a file, but allow reads from any process. On a Windows 7 machine running Java 7, I can get FileChannel's lock to work, but it…
Jason S
  • 184,598
  • 164
  • 608
  • 970
1
2 3 4 5 6 7