I’ve understood the ideas behind having a ring buffer and how it helps to not have to shift elements around in the process. However, I’m curious how one would best deal with a variable-length buffer that is thread-safe and offers similar advantages to the ring buffer? Could we double the size upon reaching capacity and have one thread do the copy-over within a mutex? Would this just variable-sized buffer just be a queue that is implemented to be thread-safe? What would be the best approach and what are the advantages and disadvantages to the alternate solutions to this type of concurrent read/write access?
2 Answers
For a multi-thread producer/consumer application, a single circular buffer usually stops being a good idea when you need it to grow.
I would usually switch to a lock-free singly-linked list of single-use FIFO buffers of fixed size, with unused buffers that can be recycled stored in a lock-free stack.
The non-bocking queue from here is simple and practical: https://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf