Reader Writer Locks
Motivation
- locks provide exclusive access to a piece of shared data
- gets rid of data races, makes reasoning about the shared data easier
- does it mean we should always protect accesses to shared data with a lock?
- what if all accesses are reads?
- what if some accesses are reads and some are writes?
- do writes need to be protected?
- do reads need to be protected?
Reader Writer Locks
- a sychronization primitive that allow for concurrent read accesses and exclusive write access
- concurrent reads: multiple readers can acquire the read lock at the same time, when read is going on no writes are allowed
- exclusive write: only one writer can acquire the write lock at a time, when write is going on, no reads or other writes are allowed
- APIs
- read_acquire, read_release, write_acquire, write_release
- when can a read_acquire succeed?
- when there's no ongoing write
- when there are ongoing reads (should this always be true?)
- when can a write_acquire succeed?
- when there's no ongoing read and write
- read-preferring vs write-preferring
- how might we implement a write-preferring reader writer lock with a monitor?
- what conditions do we need?
- how many condition variables do we need?