Definition
A ring buffer, also called a circular buffer, is a queue stored in a fixed array §14.5 whose slots are reused in a cycle: after the last slot comes slot 0. A read index marks the oldest stored item and a write index the next free slot; each store or removal advances one index by one slot. Memory is reserved once, items never move, and each operation takes constant time. A design must state how it tells full from empty, because in both states the two indexes can be equal.
The idea
A serial port receives bytes through an interrupt §6.11, a hardware signal that suspends the main program to run a short function called an interrupt handler §6.12. The handler must store each byte at once; the main program reads them later, in bursts. A plain array filled from the front runs out of room after one pass, shifting unread bytes forward wastes time, and allocating memory per byte is slow and can fail.
Bend the array into a circle, like a clock face. The writer moves clockwise, storing items, and the reader follows, removing them; the stored items are the arc between the two, and the writer reuses slots the reader has emptied. The circle creates one ambiguity: reader and writer point at the same slot both when the reader has caught up (empty) and when the writer is a full lap ahead (full).
How it works
The parts are an array of slots numbered 0 to , a read index that holds the slot of the oldest stored item, and a write index that holds the slot for the next item. To store, check that the buffer is not full, write slot , then advance . To remove, check that it is not empty, read slot , then advance . Advancing means , where is the remainder of divided by , so the slot after is 0.
Three schemes tell full from empty.
Leave one slot unused. Empty means ; full means . The writer never catches up with the reader, so can only mean empty. The capacity is .
Keep a count of stored items: 0 means empty and means full, so all slots are used. Both sides change the count, so it needs an atomic operation §9.8, one that no other code can observe half done, or a lock.
Free-running counters. and count all items ever removed and stored and are never reduced modulo ; the item with counter value is in slot . The number stored is , computed with the same unsigned wraparound §4.16 as the counters, so empty means and full means . The slots stay consecutive across a counter wrap only when is a power of two. The Linux kernel’s
kfifoworks this way.
With one producer and one consumer, schemes 1 and 3 need no lock: only the producer writes , and only the consumer writes . The order of memory accesses still matters. If the new becomes visible before the item’s bytes, the consumer reads old bytes, and the compiler may reorder accesses that way even on a single-core microcontroller §6.1 whose producer is an interrupt handler. Acquire-release ordering §9.11, from the 2011 C and C++ standards [78, 247], prevents it: the writes a thread makes before a store in release order are visible to any thread whose load in acquire order reads the stored value.
Two producers break this recipe: both can read the same , write slot and publish , losing one item. Several producers need a lock, or an atomic compare-and-swap §9.9, which changes a value only if it still holds the value read before, to claim a slot. When full, a design refuses the new item, makes the producer wait, or overwrites the oldest item. Trace buffers often overwrite to keep the newest data; the producer then also advances , breaking the rule that only the consumer writes , and the lost data must be counted, as the tracing buffer loss §14.32 entry explains.
Worked example
One slot unused, .
Start with : the buffer is empty.
Store A, B and C in slots 0, 1 and 2; becomes 3.
To store D, check , which equals : the buffer is full, with 3 items in 4 slots.
Remove A from slot 0, so . Now differs from , so store D in slot 3, and wraps to 0.
With and , slots 1, 2 and 3 hold B, C and D. The buffer is full again, since , and holds items.
Free-running 32-bit counters, . The largest 32-bit unsigned value is . Let and store three items: reaches , which wraps to . The count modulo is , correct. The items, with counter values , and 0, occupy slots 2, 3 and 0, consecutive because is a multiple of 4. With , both and the next value, 0, map to slot 0, so the second item overwrites the first.
Stated precisely
Scheme 1, with slots and indexes and . The stored items are in slots , each taken modulo , oldest first, and their number is . The buffer is empty if and only if , and full if and only if . The capacity is .
Scheme 3, with -bit unsigned counters. The number stored is , correct across a wrap because it never exceeds and . Consecutive counter values give consecutive slots across the wrap if and only if divides , that is, is a power of two. Empty holds if and only if , full if and only if . For a power of two , equals the bitwise AND of with , so slots can be computed with a mask; for other the mask is wrong.
Ordering contract for one producer and one consumer. Each index has one writer and is read and written indivisibly. The producer writes slot before storing the advanced in release order. The consumer reads slot only after a load of in acquire order shows the item, and stores the advanced in release order only after finishing the read. The producer rewrites slot only after an acquire load of shows it freed.
Limits and common mistakes
An index must be read and written in one access. On an 8-bit processor, a 16-bit index is stored as two separate bytes, and the other side can read one new byte and one old byte.
Free-running counters with a size that is not a power of two fail only at the counter wrap. At 1,000 items per second a 32-bit counter wraps after seconds, about 49.7 days, so short tests miss the fault.
Questions answered
How are full and empty states distinguished when indices match?
A ring buffer, a fixed array used as a queue whose read and write indexes wrap from the last slot to slot 0, tells full from empty by one of three rules, and matching indexes mean what the chosen rule says.
One slot left unused: matching indexes always mean empty; full is the different condition that the slot after the write index is the read index.
A separate count, changed atomically by both sides: 0 means empty and the number of slots means full.
Free-running counters, never reduced modulo the size: equal counters mean empty, and a difference equal to the size means full; the size must be a power of two.
With 4 slots and one unused, read index 1 with write index 0 means full, with 3 items, and both indexes 0 means empty. Also, the producer must write an item before publishing the new write index, and the consumer must finish reading a slot before publishing the new read index.
Related entries: Queue and FIFO (§14.14), Tracing buffer loss (§14.32), Concurrency versus parallelism (§9.1), Acquire-release ordering (§9.11), Bounded queue (§9.31). Further reading: [78, 247].