C++ Circular Queue Class

We use this C++ circular queue class in our C++ Ring Buffer Class (coming soon) that performs buffering of audio/video data streams. The underlying data transport layer is usually UDP over sockets. These programs are usually multi-threaded, using thread synchronization techniques (such as <semaphore>) to manage write/read of the data across threads.

We restrict the queue size to that of a power of two, taking advantage of the performance boost that bitwise operations have over multiplication operations. This my not be the most optimal approach for some hardware. Testing and optimization are encouraged. Also, the queue elements are allocated during initialization, so no allocations are performed during the buffering process.

The above size() member function would look shorter if it was constructed like this:

But the abs() function is typically implemented like this:

Since the operands are unsigned (never less than 0), the size() member function forgoes calling the abs() function and tests one condition of its own in place of the “n >= 0″ test in the abs() function. There is also an efficiency increase by removing the overhead of another function call.


Leave a Reply

Your email address will not be published. Required fields are marked *