title: Producer/Consumer, Stream buffer problem
body: <p>I'm trying to write a buffermanager that manages 3 Streams. The typical usage would be with a slow producer and a fast consumer. The idea behind the three buffers is that the producer ALWAYS has a buffer to write in and the consumer ALWAYS gets the latest data produced.</p>
  
  <p>Now i already have this, and it sort-off works.</p>
  
  <pre><code>namespace YariIfStream
  {
  
      /// &lt;summary&gt;
      /// A class that manages three buffers used for IF data streams
      /// &lt;/summary&gt;
      public class YariIFStream
      {
          private Stream writebuf; ///&lt;value&gt;The stream used for writing&lt;/value&gt;
          private Stream readbuf; ///&lt;value&gt;The stream used for reading&lt;/value&gt;
          private Stream swapbuf; ///&lt;value&gt;The stream used for swapping&lt;/value&gt;
          private bool firsttime; ///&lt;value&gt;Boolean used for checking if it is the first time a writebuffers is asked&lt;/value&gt;
          private Object sync; ///&lt;value&gt;Object used for syncing&lt;/value&gt;
  
          /// &lt;summary&gt;
          /// Initializes a new instance of the Yari.YariIFStream class with expandable buffers
          /// &lt;/summary&gt;
          public YariIFStream()
          {
              sync = new Object();
              eerste = true;
  
              writebuf = new MemoryStream();
              readbuf = new MemoryStream();
              swapbuf = new MemoryStream();
          }
  
          /// &lt;summary&gt;
          /// Returns the stream with the buffer with new data ready to be read
          /// &lt;/summary&gt;
          /// &lt;returns&gt;Stream&lt;/returns&gt;
          public Stream GetReadBuffer()
          {
              lock (sync)
              {
                  Monitor.Wait(sync);
                  Stream tempbuf = swapbuf;
                  swapbuf = readbuf;
                  readbuf = tempbuf;
              }
              return readbuf;
          }
  
          /// &lt;summary&gt;
          /// Returns the stream with the buffer ready to be written with data
          /// &lt;/summary&gt;
          /// &lt;returns&gt;Stream&lt;/returns&gt;
          public Stream GetWriteBuffer()
          {
              lock (sync)
              {
                  Stream tempbuf = swapbuf;
                  swapbuf = writebuf;
                  writebuf = tempbuf;
                  if (!firsttime)
                  {
                      Monitor.Pulse(sync);
                  }
                  else
                  {
                      firsttime = false;
  
                  }
              }
              //Thread.Sleep(1);
              return writebuf;
          }
  
      }
  }
  </code></pre>
  
  <p>The firsttime check is used because the first time a writebuffer is asked, it can not pulse the consumer because the buffer still has to be written with data. When a writebuffer is asked a second time, we can be sure the previous buffer contains data.</p>
  
  <p>I have two threads, one producer and one consumer.
  This is my output:</p>
  
  <pre><code>prod: uv_hjd`alv   cons: N/&lt;]g[)8fV
  prod: N/&lt;]g[)8fV   cons: 5Ud*tJ-Qkv
  prod: 5Ud*tJ-Qkv   cons: 4Lx&amp;Z7qqjA
  prod: 4Lx&amp;Z7qqjA   cons: kjUuVyCa.B
  prod: kjUuVyCa.B
  </code></pre>
  
  <p>Now it's ok the consumer lags one behind, it is supposed to do that. 
  As you can see i lose my first string of data wich is my main problem.</p>
  
  <p>The other problems are this: </p>
  
  <ul>
  <li>if i remove the firsttime check, it works. But it shouldn't in my opinion...</li>
  <li>if i add a Thread.Sleep(1); in the GetWriteBuffer() it also works. Something i don't understand.</li>
  </ul>
  
  <p>Thanks in advance for any enlightenment.</p>