0

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 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

A linked list might be better as no copy-over would be needed when extending the ring buffer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.