title: how to synchronize double buffering in multithreaded app
body: <p>I have two threads, one for data acquisition and the other one for display. In order to avoid from unnecessary synchronization. I use double buffering (or page flipping) as following:</p>
  
  <ol>
  <li>data thread is writing buffer 1 while display thread reading the buffer 2</li>
  <li>once writing is done for a buffer, data thread switches to the other buffer (buffer 2) and starts writing new page. </li>
  <li>For reading, if a buffer is in the middle of writing, display thread reads from the other buffer. </li>
  </ol>
  
  <p>It actually works well but sometimes (1 per 100 frames) I can see tearing in the display which means there is still race condition.</p>
  
  <p>So how can I implement minimal (effective) synchronization of this double buffering? A pseudo algorithm would be enough for me. </p>