title: How can I synchronize database access between a write-thread and a read-thread?
body: <p>My program has two threads:</p>
  
  <ol>
  <li>Main execution thread that handles user input and queues up database writes</li>
  <li>A utility thread that wakes up every second and flushes the writes to the database</li>
  </ol>
  
  <p>Inside the main thread, I occasionally need to make reads on the database.  When this happens, performance is not important, but correctness is.  (In a perfect world, I would be reading from a cache, not making a round-trip to the database - but let's put that aside for the sake of discussion.)</p>
  
  <p>How do I make sure that the main thread sees a correct / quiescent database?</p>
  
  <p>A standard mutex won't work, since I run the risk of having the main thread grab the mutex before the data gets flushed to the database.  This would be a big race condition.</p>
  
  <p>What I really want is some sort of mutex that lets the main thread of execution proceed only AFTER the mutex has been grabbed and released once.  Does such a thing exist?  What's the best way to solve this problem?</p>
  
  <p>UPDATE: After doing some additional research, I might use <A HREF="http://www.boost.org/doc/libs/1_35_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref" rel="noreferrer">Boost's Conditional Variable</A> to address this problem.  Either that, or just bite the bullet and cache my writes.  Thanks for the feedback!</p>