boost c++ lock-free queue vs shared queue boost c++ lock-free queue vs shared queue multithreading multithreading

boost c++ lock-free queue vs shared queue


Try both in your app, see which performs best.

Typically, polling a lock-free queue works best when the queue nearly always has entries, a blocking queue works best when the queue is nearly always empty.

The downside of blocking queues is latency, typically of the order of 2-20 uS, due to kernel signaling. This can be mitigated by designing the system so that the work done by the consumer threads on each queued item takes much longer than this interval.

The downside of non-blocking queues is the waste of CPU and memory bandwidth while polling an empty queue. This can be mitigated by designing the system so that the queue is rarely empty.

As already hinted at by commenters, a non-blocking queue is a very bad idea on single-CPU systems.


(Supplemental)

As of 1.54, you should be aware of some requirements

boost::lockfree::queue

  • T must have a copy constructor
  • T must have a trivial assignment operator
  • T must have a trivial destructor

boost::lockfree::stack

  • T must have a copy constructor

boost::lockfree::spsc_queue

  • T must have a default constructor
  • T must be copyable


You might also use a lock-free queue to avoid priority inversion in a real-time application.

For example, OpenSL on Android provides audio buffer queue callbacks on a high-priority thread. If this thread has to wait for locks held by a lower priority thread, its high-priority scheduling counts for nothing, the callbacks become irregular, and you may start to underrun your audio buffer -- which results in some unpleasant popping sounds.