In ReentrantReadWriteLock
documentation it is said:
writer can acquire the read lock, but not vice-versa
If I understand correctly it means that from the same thread you can execute:
//thread1
lock.writeLock().lock()
lock.readLock().lock()
print("this line executes")
This makes sense: if you already locked write
no other thread can enter the locked code. But if you locked read
, why can't you enter the write
block in the same thread if no other thread make read
lock? So this doesn't work:
//thread1
lock.readLock().lock()
lock.writeLock().lock()
print("this line doesn't execute")
Why do you have to unlock the read
before locking write
in the same thread?