- Readers-writers problem
In
computer science , the first and second readers-writers problems are examples of a common computing problem in concurrency. The two problems deal with situations in which many threads must access the sameshared memory at one time, some reading and some writing, with the natural constraint that no process may access the share for reading or writing while another process is in the act of writing to it. (In particular, it "is" allowed for two readers to access the share at the same time.) Areaders-writer lock is adata structure that solves one or more of the readers-writers problems.The first readers-writers problem
Suppose we have a shared memory area with the constraints detailed above. It is possible to protect the shared data behind a
mutex , in which case clearly no thread can access the data at the same time as another writer. However, this solution is suboptimal, because it is possible that a reader "R1" might have the lock, and then another reader "R2" request access. It would be foolish for "R2" to wait until "R1" was done before starting its own read operation; instead, "R2" should start right away. 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." This is also called readers-preference.--The second readers-writers problem
Suppose we have a shared memory area protected by a mutex, as above. This solution is suboptimal, because it is possible that a reader "R1" might have the lock, a writer "W" be waiting for the lock, and then a reader "R2" request access. It would be foolish for "R2" to jump in immediately, ahead of "W"; if that happened often enough, "W" would starve. Instead, "W" should start as soon as possible. This is the motivation for the second readers-writers problem, in which the constraint is added that "no writer, once added to the queue, shall be kept waiting longer than absolutely necessary." This is also called writers-preference.
The third readers-writers problem
In fact, the solutions implied by both problem statements result in starvation — the first readers-writers problem may starve writers in the queue, and the second readers-writers problem may starve readers. 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. Solutions to the third readers-writers problem will necessarily sometimes require readers to wait even though the share is opened for reading, and sometimes require writers to wait longer than absolutely necessary.
ee also
*
Producers-consumers problem
*Dining philosophers problem
*Cigarette smokers problem
*Sleeping barber problem
Wikimedia Foundation. 2010.