title: Multiple-writer thread-safe queue in C
body: <p>I am working on a multi-threaded C application using pthreads. I have one thread which writes to a a database (the database library is only safe to be used in a single thread), and several threads which are gathering data, processing it, and then need to send the results to the database thread for storage. I've seen in mentioned that it is "possible" to make a multiple-writer safe queue in C, but every place I see this mentioned simply says that it's "too complicated for this example" and merely demonstrates a single-writer safe queue.</p>
  
  <p>I need the following things:</p>
  
  <ul>
  <li>Efficient insertion and removal. I would assume that like any other queue O(1) enqueueing and dequeueing is possible.
  <li>Dynamically allocated memory, i.e. a linked structure. I need to not have an arbitrary limit on the size of the queue, so an array really isn't what I'm looking for.
  </ul>
  
  <p><b>EDIT:</b> Reading threads should not spin on an empty queue, since there is likely to be minutes worth of time with no writes, with short bursts of large numbers of writes. </p>