Iterating forward through a circular buffer without using a conditional is easy with the remainder operator...
iterator = (iterator + 1) % buffer_size;
I can't for the life of me figure out the reverse operation, iterating backward.
A circular buffer is a utility used to transfer successive data values from a producer thread to a consumer thread, who retrieves the data in FIFO (first in first out) order.
Then the output together with the partition number is written to the circular buffer in memory, which size is defined by the parameter mapreduce.
Circular buffers (also known as ring buffers) are fixed-size buffers that work as if the memory is contiguous & circular in nature. As memory is generated and consumed, data does not need to be reshuffled – rather, the head/tail pointers are adjusted. When data is added, the head pointer advances.
Thread-Safety This means the circular_buffer is not thread-safe. The thread-safety is guarantied only in the sense that simultaneous accesses to distinct instances of the circular_buffer are safe, and simultaneous read accesses to a shared circular_buffer are safe.
Does iterator = (iterator + buffer_size - 1) % buffer_size
work for you? Go one less than all the way around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With