For oscit, I am writing a read/write mutex (many concurrent readers, one writer at a time).

The tests below correspond to an implementation using a hand made read/write mutex using a semaphore and an acquire mutex.

Here are some plots of execution interleaving. A . represents a reader’s read operation. + is writer1’s signature and = is writer2.

Each thread (2 writers + readers) loops 1000 times. The maximum number of readers at once is 16.

20 readers, max 16 readers at once

20 readers

See how the readers do all their job before the writers and how these finish their tasks when all the readers have died (bottom line) ?

2000 readers, max 16 readers at once

2000 readers

With many more readers then writers and the same resource count (16), the work is more evenly distributed.

2000 readers, max 2000 readers at once

2000 readers-2

The same distribution pattern as with 20 readers appears here. This means that if the readers to max_readers ratio is too big, the writers tend to starve.

using rwlock

Tests using a posix rwlock shows better or worse results (speed) depending on the number of readers and writers. Using a typical situation (20 readers / 2 writers) shows much faster results using rwlocks.