Advantages of Reader/Writer locks on small single-core systems.

Written 2012-09-23

Tags:Latency reader-writer-lock contention semaphore lock 


Most commonly, reader/writer locks are applied to larger systems where it can decrease contention when there are many threads trying to read. In my case there were actually very few threads, but a reader/writer lock still proved advantageous. Let us examine what can happen when two threads need to read, using both a standard lock, and a reader/writer lock.

Premise: Thread A is high-priority, thread B is low-priority, and the OS provides hard-priority-scheduling. Only these two threads will run in the period of time we're interested in.

Here's what could happen using a single standard lock.
  1. An event occurs, causing thread B to run.
  2. Thread B takes the lock and begins reading
  3. An event occurs, causing thread A to run.
  4. A context-switch occurs due to thread-priority. Thread A is now running.
  5. Thread A attempts to read but is blocked.
  6. A context-switch occurs, allowing thread B to release the lock.
  7. A context-switch occurs due to thread-priority. Thread A is now running.
  8. Thread A takes the lock, reads, unlocks, and blocks elsewhere.
  9. A Context switch occurs due to only one ready-to-run thread.
  10. Thread B continues until it blocks elsewhere.

But now, lets look at what happens with a reader/writer lock instead, with similar event timing.
  1. An event occurs, causing thread B to run.
  2. Thread B takes the lock and begins reading
  3. An event occurs, causing thread A to run.
  4. A context-switch occurs due to thread-priority. Thread A is now running.
  5. Thread A also takes the lock, reads, and releases the lock. It then blocks elsewhere.
  6. Thread B finishes reading, releases the lock, and blocks elsewhere.

First we notice that using the reader/writer lock avoids a pair of unceccesary context-switches. This is nice, but the advantage is heavily architecture/OS dependent.

Even assuming the context-switches are cheap, there's another advantage to the reader/writer lock in this situation. The advantage is system latency - by using the reader/writer lock instead of a simple lock, high-priority threads no longer have to wait for low-priority thread to release the lock. The ability to allow multiple threads to enter the critical section is the same reason it is often chosen in high-contention environments, but it also usefully provides reduced latency when experiencing any contention.