Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

best way to upgrade read lock to write lock?  RSS feed

 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the java.util.concurrent.locks.ReentrantReadWriteLock
and the code to upgrade a lock from read to write has a very ugly
finally to unlock the last read. There has to be a better way.

The basic code looks like


It sure seems silly to issue the read lock (after the finally w.unlock)
just to clear it in the finally, but I can't find a cleaner structure
that works.


Ideas and hints greatly appreciated.
Pat
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you acquiring the read lock at all? It looks like the only thing you do with the read lock is check the status of initialized. Why do you need a lock for that? For thread safely, you can just make the boolean volatile and forget about the read lock. Or is there other code we need to know about?
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you suspect, the real code is more complex. I simplified it for posting.

So I'd really like a better approach.

Thanks
Pat
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sure seems silly to issue the read lock (after the finally w.unlock) just to clear it in the finally, but I can't find a cleaner structure that works.


Issuing a read lock and freeing the write lock is actually downgrading the lock, and not upgrading the lock. To do that, make sure that you acquire the read lock *before* you release the write lock -- otherwise, it will be possible for another thread to acquire the write lock in between the period between freeing the write lock and acquiring the read lock.

Henry
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct if you look at the end of the code snippet.
But at the top, its upgrading from the read only lock to write.

The


is needed to properly clear the first lock of the read.
I can't get the code to work if I switch the order at the bottom, to what would appear to me to be natural:


This order hangs. It would seem to be more 'natural' in that you could make sure no one else updates it until you do the


two or three lines later.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API for ReentrantReadWriteLock states that downgrading from a write lock to a read lock is possible, but upgrading is not. Which means it's not possible to get a write lock while you have a read lock; you need to release the read first. This makes sense - what if you had two threads with read locks, and both tried to upgrade? Neither could acquire the write lock until the other gave up the read lock, and that wouldn't happen unless the coder explicitly unlocked the read lock before acquiring the write lock. In which case it's not really an upgrade, it's a read lock followed by no lock followed by a write lock. That's just what you have to do.

[Pat]: It sure seems silly to issue the read lock (after the finally w.unlock) just to clear it in the finally, but I can't find a cleaner structure that works.

Well, aren't you done with the reading at that point anyway? I would move the final read unlock() up before the write lock stuff, and then it's simpler:


I repeated the check of initialized because it might have happened while you were waiting for the lock, and there seems no need to initialize twice in that case. And I'm assuming initialized is declared as volatile, so we can still check its value outside synchronized or locked code.
[ August 12, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!