title: How to prevent thread starvation
body: <p>I am writing a zero-latency cloud gaming server. It's a software pipeline. In the first stage we capture the screen and in the second stage we encode it to the video. </p>
  
  <p>However after some amount of time the second stage freezes. I tried many platform-indepandent approach, but in vein, either of them will freeze eventually. The answer of <a href="https://stackoverflow.com/questions/15910759/how-to-prevent-threads-from-starvation-in-c11">How to prevent threads from starvation in C++11</a> stated that we should use mutex. I tried it. And it can last longer but it still freeze sometimes(rarely). I think mutex is not a explicit hint to prevent thread starvation too. (maybe I am doing wrong?)</p>
  
  <p>Now I use mutex and disable Windows priority boost feature at the same time, but I don't like this solution at all. could anyone provide an example of starvation-free producer and consumer(better in C++11)?</p>
  
  <p>producer:</p>
  
  <pre><code>while(Streamer.IsConnected()) {
      uint8_t *pBits = Streamer.AcquireNext();
      // The buffer is full
      if(pBits &amp;&amp; get_counter(&amp;fps_limiter) &gt;= 1000 / args.m_MaxFps &amp;&amp; check_target_window(args.m_TargetWindow.c_str(), limit, &amp;rect)) {
          BROFILER_FRAME("MainLoop")
          start_counter(&amp;fps_limiter);
          if(!FAILED(capture_screen(g_DXGIManager, rect, pBits)))
              Streamer.PushNext();
      }
      else {
          this_thread::yield();
          // lower cpu usage
          Sleep(1);
          continue;
      }
  
      if (get_counter(&amp;bit_rate) &gt;= 1000) {
          uint32_t bps = Streamer.GetBitRate();
          printf("\rBirate: %u bps, %u Bps\t\t\t\t\t", bps, bps/8);
          start_counter(&amp;bit_rate);
      }
  }
  </code></pre>
  
  <p>consumer:</p>
  
  <pre><code>    while(!m_ServerShouldStop) {
          uint8_t *data = AcquireLast();
          if (!data) {
              this_thread::yield();
              Sleep(1);
              continue;
          }
          // encoder callback
          uint8_t *out;
          uint32_t size = m_Encoder(data, &amp;out);
  
          PopLast();
  
          // If encoder output something, send it immediately
          if(size&gt;0) {
              // send the size of buffer
              int res1 = ::send_whole_buffer(client_sck, reinterpret_cast&lt;uint8_t *&gt;(&amp;size),
                  sizeof(size));
              // then the contents
              int res2 = ::send_whole_buffer(client_sck, out, size);
  
              bytes += size;
  
              if (m_EventHandler)
                  m_EventHandler-&gt;onFrameSent();
  
              // If any of them fails....
              if(!res1||!res2)
                  break;
          }
          if (get_counter(&amp;counter) &gt;= 1000) {
              m_Bps = bytes * 8;
              bytes = 0;
              start_counter(&amp;counter);
          }
  
      }
  ...
  </code></pre>
  
  <p>Initially I did not do any protection to circular queue. I think there no race condition(one producer and one consumer). Then I try to add mutex but nothing change....</p>