title: Memory management in a lock free queue
body: <p>We've been looking to use a lock-free queue in our code to reduce lock contention between a single producer and consumer in our current implementation. There are plenty of queue implementations out there, but I haven't been too clear on how to best manage memory management of nodes.</p>
  
  <p>For example, the producer looks like this:</p>
  
  <pre><code>queue.Add( new WorkUnit(...) );
  </code></pre>
  
  <p>And the consumer looks like:</p>
  
  <pre><code>WorkUnit* unit = queue.RemoveFront();
  unit-&gt;Execute();
  delete unit;
  </code></pre>
  
  <p>We currently use memory pool for allocation. You'll notice that the producer allocates the memory and the consumer deletes it. Since we're using pools, we need to add another lock to the memory pool to properly protect it. This seems to negate the performance advantage of a lock-free queue in the first place.</p>
  
  <p>So far, I think our options are:</p>
  
  <ul>
  <li>Implement a lock-free memory pool.</li>
  <li>Dump the memory pool and rely on a threadsafe allocator.</li>
  </ul>
  
  <p>Are there any other options that we can explore? We're trying to avoid implementing a lock-free memory pool, but we may to take that path.</p>
  
  <p>Thanks.</p>