A fixed-capacity, STL-compliant ring buffer for C++03 and later.
| Branch | CI | Documentation |
|---|---|---|
master |
||
develop |
A circular buffer, also called a ring or cyclic buffer, holds a fixed number of elements. When it is full, each new element overwrites the oldest one.
boost::circular_buffer is a standard container that behaves this way. It
works like std::deque or std::vector, with random-access iterators and
constant-time insertion and removal at either end, and it works with the
standard algorithms. Once its capacity is set, it doesn't allocate memory
until you change the capacity yourself.
#include <boost/circular_buffer.hpp>
int main()
{
// Create a circular buffer with a capacity for 3 integers.
boost::circular_buffer<int> cb(3);
cb.push_back(1);
cb.push_back(2);
cb.push_back(3); // The buffer is full: 1 2 3
cb.push_back(4); // Overwrite 1 with 4: 2 3 4
cb.push_back(5); // Overwrite 2 with 5: 3 4 5
cb.pop_back(); // 5 is removed: 3 4
cb.pop_front(); // 3 is removed: 4
return cb[0] == 4 ? 0 : 1;
}- Header-only. There's nothing to build or link.
- C++03 and later. CI tests C++03 through the latest standard with GCC, Clang and MSVC.
- Fixed memory. The buffer doesn't allocate behind your back, so it suits real-time and performance-critical code.
- STL-compliant. It has random-access iterators, allocator support and
move semantics, and works with the
<algorithm>functions. - Contiguous storage. The elements are held in at most two contiguous
pieces.
array_one(),array_two()andlinearize()give C APIs direct access to them. - Debug support. With
BOOST_CB_ENABLE_DEBUG=1, using an invalidated iterator triggers an assertion.
There's also boost::circular_buffer_space_optimized, which takes the same
fixed maximum capacity but allocates memory only as it needs it, and frees it
again as the buffer shrinks.
- Keeping the most recent N samples, readings or log lines.
- A cache of the last N items inserted.
- A fixed-capacity FIFO or LIFO queue.
- The storage for a bounded producer/consumer queue. See
example/circular_buffer_bound_example.cpp.
Like the standard containers, circular_buffer isn't thread-safe. If several
threads share one buffer, you have to provide the locking yourself.
Boost.CircularBuffer is part of Boost. If you have
Boost installed, you already have it: just include
<boost/circular_buffer.hpp>.
With CMake:
find_package(Boost REQUIRED)
target_link_libraries(my_app PRIVATE Boost::headers)Within the Boost superproject, link Boost::circular_buffer instead.
The library depends only on other header-only Boost libraries: Assert, ConceptCheck, Config, Core, Move, ThrowException and TypeTraits.
- Documentation: introduction, rationale, examples and the full reference.
example/: small programs you can compile.
From a Boost superproject checkout, with this repository at
libs/circular_buffer:
./b2 libs/circular_buffer/test cxxstd=03,11,17Or as a standalone CMake project against an installed Boost (1.85 or newer, CMake 3.10 or newer):
cmake -S . -B build -G Ninja -DBUILD_TESTING=ON
ninja -C build check # build and run the tests
ninja -C build check-valgrind # the same under valgrind| Branch | Purpose |
|---|---|
master |
The latest release. Builds with the master branch of the Boost superproject. |
develop |
Work for the next release. Pull requests go here. |
Bug reports, questions and pull requests are welcome on
GitHub. Please send pull
requests to develop, and include tests for both containers where they apply.
Distributed under the Boost Software License, Version 1.0.
Originally written by Jan Gaspar (2003–2008).
The Boost logo is a trademark of The C++ Alliance, used according to the Boost Logo Policy.