public void getWriteLock()
while(givenLocks != 0)
givenLocks = -1;
When requesting a write lock, a thread signals that it is waiting by increment-
ing the waitingWriters variable, waits until no more readers are holding the
lock, and then takes a lock by setting givenLocks to -1 and decrements wait-
ingWriters to signal that it is no longer waiting:
public void releaseLock();
if(givenLocks == 0)
if(givenLocks == -1)
givenLocks = 0;
To release a lock, the protocol is followed in reverse. To use this lock, an appli-
Create a lock for each critical resource to be protected.
Request the lock for read before reading from the resource.
Release the lock for read after reading from the resource.
Request the lock for write before writing to the resource.
Release the lock for write after writing to the resource.