0

In Wikipedia: CircularBuffer section "Difficulties->Always keep one slot open", the author does not mention any requirement of synchronization between threads:

Always keep one slot open

This design always keeps one slot unallocated. A full buffer has at most (size-1) slots. If both pointers refer to the same slot, the buffer is empty. If the end (write) pointer refers to the slot preceding the one referred to by the start (read) pointer, the buffer is full.

The advantage is:

  • The solution is simple and robust.

The disadvantages are:

  • One slot is lost, so it is a bad compromise when the buffer size is small >or the slot is big or is implemented in hardware.
  • The full test requires a modulo operationBlockquote

Why is there no such requirement? The order of execution should be:

  1. Insert new data

  2. Increment the write index

However, what if the compiler reverses the order as an optimization?

Benji Mizrahi
  • 2,154
  • 2
  • 23
  • 38
  • Most of the article is fairly neutral when it comes to such threading issues, not sure why you've focussed on just one model - and, indeed, there are circumstances where a circular buffer may be used by just a single thread and so of course no threading issues may exist. – Damien_The_Unbeliever Mar 23 '15 at 09:09
  • This will be required method of communication between two company's communication channel on a multi-core system (using shared memory). If you read other options, the author mentions what to do when it is used by multiple threads. – Benji Mizrahi Mar 23 '15 at 10:00
  • None of the options discuss the required memory barriers or write orders to be thread safe. There are two places where synchronization is mentioned, but that's in the context of multiple writers to the same variables. In short, yes, you need to consider thread safety if you're implementing *any* of these options, above and beyond what little is mentioned on that page. – Damien_The_Unbeliever Mar 23 '15 at 10:47

0 Answers0