title: readers-writers problem that gives stronger priority to readers
body: <p>I'm reading a book which talks about a solution about readers-writers problem that  gives stronger priority to readers where a writer leaving its critical section will always restart a waiting reader if one exists. Below is the solution code:</p>
  <pre><code>int readcnt;      /* Initially = 0 */
  sem_t mutex, w;   /* Both initially = 1 */
  
  void reader(void)
  {
      while (1) {
          P(&amp;mutex);
          readcnt++;
          if (readcnt == 1)
              P(&amp;w);
          V(&amp;mutex);
          
          /* Critical section */
          /* Reading happends */
          
          P(&amp;mutex);
          readcnt--;
          if (readcnt == 0) /* Last out */
              V(&amp;w);
          V(&amp;mutex);
      }
  }
  
  void writer(void)
  {
      while (1) {
          if (readcnt &gt; 1)  //&lt;-----could it be readcnt == 1?
              continue;
          P(&amp;w);
          
          /* Critical section */
          /* Writing happends */
          
          V(&amp;w);
      }
  }
  </code></pre>
  <p>I have two questions:</p>
  <p>Q1-in the check condition <code>if (readcnt &gt; 1)</code> in <code>write</code> method, can the condition be <code>if (readcnt == 1)</code>? I can see the <code>readcnt</code> is always one if there are readers waiting and get blocked.</p>
  <p>Q2- skip the while loop (<code>continue</code>) in <code>writer</code> method can work but doesn't look like a native perfect approach, because you rely on the manual controlling <code>continue</code> to iterate until a reader is chosen to be restarted, image that there are 100 writer and 100 readers,  the code might need to skip 100 times then restarts a reader and theoretically, those 100 wirter can be chosen to be restarted over and over again but skipped each time just like a infinite loop, which is a huge cost to cpu resource, is it a native approach for this?</p>