Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>mutex 1</code> protects the <code>readcount</code> variable; <code>mutext 2</code> protects <code>writecount</code> variable; mutex <code>r</code> protects the reading operations and mutext <code>w</code> protects the writing operations.</p> <p>1) Let's suppose a writer comes in:</p> <p>Signals <code>mutex 2</code> and increments <code>writercount</code> to account for the extra writer (itself) Since it is the only process that can change <code>writercount</code> (as it is holding <code>mutex 2</code>), it can safely test whether it is the only writer (<code>writercount==1</code>), if true, it signals mutex <code>r</code> to protect readers from coming in -- other writers (<code>writercount &gt; 1</code>) can enjoy the mutex <code>r</code>being signaled already.</p> <p>The writer then signals mutex <code>w</code> to protect its changes from other (concurrent) writers.</p> <p>Last writer (<code>writecount==1</code>) releases mutex <code>r</code> to let readers perform their tasks.</p> <p>2) Let's suppose a reader comes in:</p> <p>Signals <code>mutex 3</code> to protect the readers' setup logic from other readers; then signals mutex <code>r</code> to protect from other writers (remember, <code>r</code> is signaled while writers are operating); then signals <code>mutex 1</code> to protect readcount (from other readers that might be exiting) and if it is the first reader (<code>readercount == 1</code>), signals mutex <code>w</code> to protect from writers (now excludes writers from performing their operations).</p> <p>Reading can be done parallel, so no protection is needed from other readers while reading (remember, mutex <code>w</code> is being held at this point, so no intereference from writers)</p> <p>Then the last reader resets the write mutex (<code>w</code>) to allow writers.</p> <hr> <p>The trick that prevents writer starvation is that writers pose as readers (when signaling mutex <code>p</code>), so have a good chance of getting scheduled even when there are many readers. Also, <code>mutex 3</code> prevents too many readers from waiting on mutex <code>r</code>, so writers have a good chance to signal <code>r</code> when they come.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload