[{"data":1,"prerenderedAt":258},["ShallowReactive",2],{"project-27412233":3},{"id":4,"name":5,"line":6,"line_source":7,"title":8,"url":9,"domain":10,"maker":11,"posted_at":12,"year":13,"points":14,"comments":15,"rank_in_year":16,"shown_in_year":17,"top":18,"tier":19,"category":20,"subcategory":21,"format":22,"keywords":23,"reception":28,"sentiment":29,"stance_mix":30,"scored_comments":34,"aspects":35,"strip":41,"post_text":43,"what":44,"said":65,"maker_in_thread":135,"shape":138,"grid":141,"attempts":219,"same_week":220,"maker_shows":37,"maker_hidden":250,"reader_version":251,"card":252},"27412233","Embedded Ring Buffer","Embedded Ring Buffer C++98","title","Show HN: Embedded Ring Buffer C++98","https:\u002F\u002Fgithub.com\u002FBambofy\u002FEmbeddedRingBuffer","github.com","Bambo",1622977138,2021,26,37,1229,2533,49,"shown","languages","ring buffer library","library",[24,25,26,27],"ring buffer","c++98","embedded","circular buffer","roasted",-0.444,{"positive":31,"neutral":32,"mixed":31,"critical":33},0,5,8,13,{"execution":36,"idea":38,"design":40},{"positive":31,"critical":37},7,{"positive":31,"critical":39},1,{"positive":31,"critical":31},[39,39,42,31,39,31,31,31,31,31,31,31,31,31,31,31,31,31,31,39,31,31,31,31,39,39,31,39,39,31,42,42,42,42,31,31,31],3,null,{"problem":43,"audience":45,"pricing":43,"motivation":43,"time_to_build":43,"stack":57,"openness":64,"stage":64,"team_size":64},{"value":46,"source":47,"evidence":48},"Embedded C++ developers","inferred",[49,53],{"id":50,"by":51,"text":52},"27419188","john_fushi","> memset(data, 0, LENGTH); >\u002F\u002F ... > 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.…",{"id":54,"by":55,"text":56},"27419303","ncmncm","The ring buffer is the tool of champions. But confining yourself to C++98 is just masochism. Just because we're embedded doesn't mean we can't have nice things.",[58],{"value":59,"source":60,"evidence":61},"C++98","maker",[62],{"id":63},"post","unknown",{"praise":66,"objections":67,"requests":108,"questions":125,"alternatives":130},[],[68,87,95,101],{"value":69,"source":70,"evidence":71},"Uses volatile incorrectly; lacks compiler and memory barriers for thread or interrupt safety","commenter",[72,76,80,84],{"id":73,"by":74,"text":75},"27420419","ghhhhhk8899jj","volatile does not imply compiler fence on gcc, so this code has a race condition when updating read\u002Fwrite_position, you need compiler barriers here >data[write_position] = value;…",{"id":77,"by":78,"text":79},"27420087","gpderetta","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.",{"id":81,"by":82,"text":83},"27421196","tialaramex","> 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\"…",{"id":85,"by":78,"text":86},"27423125","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…",{"value":88,"source":70,"evidence":89},"memset of whole array is wrong for non-trivial types; std::fill preferred",[90,91],{"id":50,"by":51,"text":52},{"id":92,"by":93,"text":94},"27419289","dataflow","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?!)",{"value":96,"source":70,"evidence":97},"Overrun case does not write, which is unusual for a general ring buffer API",[98],{"id":99,"by":51,"text":100},"27419385","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. \u002F* * @brief Retrieve a…",{"value":102,"source":70,"evidence":103},"NULL should not be used in C++",[104],{"id":105,"by":106,"text":107},"27419146","mhogomchungu","Don't use \"NULL\" in C++[1]. [1] https:\u002F\u002Fcpp4arduino.com\u002F2018\u002F10\u002F26\u002Fwhy-cpp-programmers-dont-use-null.html",[109,115],{"value":110,"source":60,"evidence":111},"Review code for correctness and bugs",[112],{"id":113,"by":11,"text":114},"27412234","Looking for CC, advice and bugs!",{"value":116,"source":70,"evidence":117},"Simplify wrap-around handling with a virtual ring buffer or power-of-two counters",[118,122],{"id":119,"by":120,"text":121},"27419245","keithwinstein","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…",{"id":123,"by":55,"text":124},"27419341","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,…",[126],{"value":127,"source":70,"evidence":128},"Where is the skip parameter in Read?",[129],{"id":99,"by":51,"text":100},[131],{"value":132,"source":70,"evidence":133},"Virtual memory ring buffer (Stanford stagecast implementation)",[134],{"id":119,"by":120,"text":121},{"comments":39,"answered":31,"key":136},[137],{"id":113,"text":114},{"commenters":139,"max_depth":33,"maker_replies":39,"first_comment_mins":31,"lifespan_hours":140},16,122,[142,144,145,149,152,153,155,157,159,161,163,165,168,170,172,174,176,179,181,183,186,187,189,191,193,194,196,199,201,202,204,206,207,210,212,214,217],[50,31,39,143,51,31],"project\u002Fcritical\u002Fexecution",[92,31,39,143,93,31],[146,31,42,147,148,31],"27419610","project\u002Fneutral\u002Fexecution","klodolph",[150,31,31,151,93,31],"27419648","commenter\u002Fneutral\u002Fnone",[77,31,39,143,78,31],[154,31,31,151,55,31],"27421902",[156,31,31,151,78,31],"27422519",[158,31,31,151,55,31],"27422672",[160,31,31,151,78,31],"27423066",[162,31,31,151,148,31],"27425921",[164,31,31,151,78,31],"27426498",[166,31,31,151,167,31],"27420092","pjmlp",[169,31,31,151,93,31],"27420300",[171,31,31,151,148,31],"27420403",[173,31,31,151,93,31],"27420417",[175,31,31,151,78,31],"27420703",[177,31,31,151,178,31],"27472626","MaxBarraclough",[180,31,31,151,148,31],"27420317",[182,31,31,151,167,31],"27420538",[184,31,39,143,185,31],"27420557","bjornsing",[81,31,31,151,82,31],[188,31,31,151,55,31],"27421782",[190,31,31,151,148,31],"27425824",[192,31,31,151,82,31],"27428132",[99,31,39,143,51,31],[54,39,39,195,55,31],"project\u002Fcritical\u002Fidea",[197,39,31,151,198,31],"27419455","R0b0t1",[73,200,39,143,74,31],2,[85,200,39,143,78,31],[113,42,31,203,11,39],"maker\u002Fneutral\u002Fnone",[119,42,42,205,120,31],"project\u002Fneutral\u002Fdesign",[123,42,42,147,55,31],[208,42,42,147,209,31],"27420608","injinj",[105,211,42,147,106,31],4,[213,211,31,151,93,31],"27419267",[215,211,31,151,216,31],"27419556","imron",[218,211,31,151,78,31],"27420352",[],[221,226,231,236,241,245],{"id":222,"name":223,"points":224,"category":225},"27421408","ToolJet",429,"web_saas",{"id":227,"name":228,"points":229,"category":230},"27414677","SectorTradingAlgorithm",249,"finance_crypto",{"id":232,"name":233,"points":234,"category":235},"27434254","nettu-meet",154,"learning_science",{"id":237,"name":238,"points":239,"category":240},"27408326","Rustpad",143,"dev_tools",{"id":242,"name":243,"points":244,"category":225},"27379310","Vistimo Quotes",136,{"id":246,"name":247,"points":248,"category":249},"27384949","Spot",125,"productivity",false,"v2",{"id":253,"name":5,"line":6,"ai":250,"cat":20,"year":13,"points":14,"comments":15,"rec":28,"s":254,"top":18,"kw":255,"maker":11,"domain":10,"tier":19,"attempts":31,"strip":256,"url":9,"mix":257,"praise":-1,"objection":69,"sub":21},27412233,-0.44,[24,25,26,27],"1130100000000000000100001101103333000",[31,32,31,33],1793991029126]