Embedded Ring Buffer

Languages and libraries

ring buffer library

SHOW HN2021

26points
37comments
#1,229of 2,533 in 2021
Top 49%of 2021 by points

How HN took it

RoastedHalf or more of the comments are critical

0 positive, 5 neutral or asking, 0 mixed, 8 critical, out of 13 comments about the project. Thread score -0.44.

Execution: 0 for, 7 againstIdea: 0 for, 1 against

The thread, comment by comment

Each row is a top-level comment and its replies, in HN's order. Select a cell to open that comment on HN.

positive neutral or mixed critical about another commenter or something else the maker

What HN said

Objections

  • Uses volatile incorrectly; lacks compiler and memory barriers for thread or interrupt safety

    From 4 comments
    • volatile does not imply compiler fence on gcc, so this code has a race condition when updating read/write_position, you need compiler barriers here >data[write_position] = value;…
      ghhhhhk8899jj on HN
    • Unfortunately the compiler can still reorder nonvolatile operations across volatile ones, so unless the buffer is also volatile, it is not as useful as one would expect.
      gpderetta on HN
    • > The use of volatile is typical here. This is definitely true. C++ programmers typically do this. > It allows the ring buffer to be used from interrupts Unfortunately "allows"…
      tialaramex on HN
    • exactly. The fence is required even when communicating with a signal handler (or interrupt handler) on the same thread. For for the multithreaded case, depending on the…
      gpderetta on HN
  • memset of whole array is wrong for non-trivial types; std::fill preferred

    From 2 comments
    • > memset(data, 0, LENGTH); >// ... > T data[LENGTH]; I'm not sure how important it is in practice, but I'm pretty sure you don't zero out the whole array for sizeof(T) > 1.…
      john_fushi on HN
    • Ouch, yeah. They need std::fill(). This is the kind of thing that makes you lose faith in a C++ library. (Also, what's with the volatile private variables?!)
      dataflow on HN
  • Overrun case does not write, which is unusual for a general ring buffer API

    From 1 comment
    • N.B. I'm writing my comments as I am reading your code. Please, don't take offense from my criticism, it is meant to be constructive, albeit concise. /* * @brief Retrieve a…
      john_fushi on HN
  • NULL should not be used in C++

    From 1 comment
    • Don't use "NULL" in C++[1]. [1] https://cpp4arduino.com/2018/10/26/why-cpp-programmers-dont-use-null.html
      mhogomchungu on HN

Requests

  • Review code for correctness and bugs

    From 1 comment
  • Simplify wrap-around handling with a virtual ring buffer or power-of-two counters

    From 2 comments
    • Looks reasonable! On a system with virtual memory, one popular trick is the "virtual ring buffer," which lets the reader and writer always access a contiguous region of the full…
      keithwinstein on HN
    • That is a very good idea. You don't even need to double-map memory. Just make the ring buffer smaller than your actual buffer, and treat any overrun as if it had wrapped around,…
      ncmncm on HN

Questions

  • Where is the skip parameter in Read?

    From 1 comment
    • N.B. I'm writing my comments as I am reading your code. Please, don't take offense from my criticism, it is meant to be constructive, albeit concise. /* * @brief Retrieve a…
      john_fushi on HN

Alternatives named

  • Virtual memory ring buffer (Stanford stagecast implementation)

    From 1 comment
    • Looks reasonable! On a system with virtual memory, one popular trick is the "virtual ring buffer," which lets the reader and writer always access a contiguous region of the full…
      keithwinstein on HN

Category, keywords, summaries and labels on this page were made by AI (Thread Reader v2) and not checked by hand. Each point links to the comments it came from.

Add a badge to your READMEShown on HackerShow
[![Shown on HackerShow](https://hackershow.org/badge.svg)](https://hackershow.org/show/27412233)