1

I am converting a code with reentrant locks into stamp locks. The code consists of a scenario where readlock is inside the writelock as following example.

private final ReadWriteLock lock = new ReentrantReadWriteLock();

A() {
    lock.writeLock().lock();
    ...
    B();
    ...
}

B () {
   lock.readLock().lock();
   ...
}

The stamp locks are not reentrant. Then if I change readLock() into tryOptimisticRead() and writeLock() with writeLock() in stamp locks, there may be a deadlock scenario.

Hence, I want to know how to handle this kind of scenarios with stamp locks.

Janaka
  • 481
  • 4
  • 14
  • 3
    Sounds really risky. If you have to ask, then you probably shouldn't be getting rid of the safer `ReentrantLock`. – Kayaman Feb 25 '21 at 12:32
  • 1
    I agree that you should probably stay with the ordinary reentrant lock. Besides that, the solution is very simple: separate code dealing with the locks and the (private) code dealing with the data. The latter must be invoked by the former when holding the appropriate locks. – Holger Feb 25 '21 at 14:00

0 Answers0