5

I am looking for a thread-safe C/C++ queue implementation that is optimized for the push operation. I don't mind if the pop operation blocks but I would like to never be blocking on the push side.

Let me explain why. I am planning on writing a profiler for a C# application and I will have multiple threads pushing messages onto a single dispatcher thread. I don't mind if the dispatcher blocks shortly, but I would like to avoid any delay on the push side.

taylorjonl
  • 869
  • 9
  • 15
  • 1
    There's a general multi-reader, multi-writer non-blocking queue by [Fober, Orlarey and Letz](http://www.grame.fr/Ressources/pub/LockFree.pdf), though it has the usual problem of lockfree programming that it cannot release the memory of popped elements. I can imagine that if you're willing to add a lock on the dequeue side, you might be able to overcome that. If you only need a single consumer, then it's much easier. – Kerrek SB Nov 26 '12 at 08:45

1 Answers1

3

You can use boost.lockfree. It's in boost sandbox svn and planned to be released with boost for version 1.53 or 1.54 depending on whether or not boost.atomic get released in time. For the moment boost.lockfree depends on std::atomic and not boost.atomic, so you need a c+11 compiler to use it.

geekpp
  • 511
  • 2
  • 6
  • I will look into this later today. I may be constrained though, my team uses VS2010 which doesn't support atomics, we are working on getting a copy of VS2012 but that may not happen for a while. So I may end up using an older version. – taylorjonl Nov 26 '12 at 16:34
  • Thank you for your comment, I read a bit more, that library looks promising. I found this link: http://boost-sandbox.sourceforge.net/doc/html/lockfree.html It has some very good info. I actually think I am going to use the http://boost-sandbox.sourceforge.net/doc/html/boost/lockfree/spsc_queue.html. I will give each thread a dedicated queue and have a single thread pulling from the queue, then flushing the contents down a pipe or putting it in a SQL database. – taylorjonl Nov 27 '12 at 05:32
  • For anyone reading this later, read this comment carefully, lockfree doesn't exist in 1.52(current release as of today). Make sure you have >=1.53 or you may waste a couple hours like I did, my fault for not reading careful enough :) – taylorjonl Nov 28 '12 at 06:20