On the other hand, consider if both A and B try to upgrade to a write lock. This eventually happens, and at that point thread A acquires a write lock (and, eventually, thread A will then relinquish this lock). This means it must wait for B and C to relinquish their read lock. ![]() Thread A tries to upgrade to a write-lock. There is no possibility of deadlock in this case.įor instance, suppose there are three threads - A, B, and C, all which have read-locked the lock. So, there is no conceptual problem with a single thread upgrading its read lock to a write lock it just needs to wait until all other holders of the read lock relinquish it before the upgrade can occur. Upgrading from a read-lock to a write-lock is not about acquiring one lock when another is already held rather, it's about changing the type of lock which is already held on a single lock. Trying to acquire the write lock with the read lock held is deadlocky.Ī write lock is stronger than a read lock it is like a read lock with an additional property (that no other thread may also hold the lock). If a reader tries to upgrade it's not released the read lock yet. It can never be both read- and write- locked at the same time. The read-write lock can be read-locked by one or more threads concurrently, or it can be write-locked. ![]() Rather than thinking that ReadWriteLock's readLock method and writeLock method return actual locks, consider that what they actually do is return an abstract mechanism allowing acquisition of different types of lock (on the same, single, read-write lock mechanism).Ī read-write lock (mechanism) is a single lock which can be locked in two ways: read-locked and write-locked. (This is all confused a little by the overloading of the term "lock" - it is both a verb and a noun, that is, one can lock a lock). That's not the correct way to think about it, even though the API seems to expose such a composition (by providing methods to obtain a reference to the "write lock" and to the "read lock"). ![]() It sounds like you are considering a "read lock" and a "write lock" to be two different locks which compose a read-write lock.
0 Comments
Leave a Reply. |