Readers–writers problem

[1] There are at least three variations of the problems, which deal with situations in which many concurrent threads of execution try to access the same shared resource at one time.

This is the motivation for the first readers–writers problem, in which the constraint is added that no reader shall be kept waiting if the share is currently opened for reading.

Once the reader is done executing the entry section, it will unlock it by signalling the mutex.

The reader to finish last (indicated by the readcount variable) must unlock the resource, thus making it available to writers.

This means that a stream of readers can subsequently lock all potential writers out and starve them.

This is accomplished by forcing every reader to lock and release the readtry semaphore individually.

The very last writer must release the readtry semaphore, thus opening the gate for readers to try reading.

No reader can engage in the entry section if the readtry semaphore has been set by a writer previously.

The reader must wait for the last writer to unlock the resource and readtry semaphores.

Their sole purpose is to avoid race conditions on the readers and writers while they are in their entry or exit sections.

Therefore, the third readers–writers problem is sometimes proposed, which adds the constraint that no thread shall be allowed to starve; that is, the operation of obtaining a lock on the shared data will always terminate in a bounded amount of time.

A solution with fairness for both readers and writers might be as follows: This solution can only satisfy the condition that "no thread shall be allowed to starve" if and only if semaphores preserve first-in first-out ordering when blocking and releasing threads.