Reputation: 1049
From oracle:
Whether or not a read-write lock will improve performance over the use of a mutual exclusion lock depends on the frequency that the data is read compared to being modified, the duration of the read and write operations, and the contention for the data - that is, the number of threads that will try to read or write the data at the same time.
As a beginner, I need someone's help to help me clear some things. Text in quote makes sense, because in general why would we want one reader thread to wait for another reader thread? In this way, using ReadWriteLock
, we can have concurrency in readers and not have complete mutual exclusion between every single thread.
So I thought, why not always use ReadWriteLock
instead of conventional
locks? No matter if I have little readers, there could come a situation when two readers try to access a shared resource at the same time. But that sounds too easy, plus why are those rules that tell me when to pick ReadWrite
at all?
Can someone tell me what am I missing? In my opinion, I would always prefer ReadWrite
against conventional
locks, because although there could be little readers/not long operation/little thread contentions, there could still come a scenario where I could profit with that "little given concurrency" for readers.
Upvotes: 2
Views: 326