Code Monkey home page Code Monkey logo

atomicringbuffer's Introduction

AtomicRingBuffer

A Lock-Free Ring Buffer for single-sender-single-receiver usecases.

AtomicRingBuffer manages a section of memory as a round-robin byte-buffer. It is intended to be used in a way that minimizes copying data - a writer can request a buffer section to write to and a reader can directly read published data from the buffer.

Synchronization is based on a set of std::atomic<> variables. This means that operations themselves are not synchronized with each other. If multiple operations are performed at performed concurrently, they may spuriously fail in case of a race condition. It is guaranteed that failing operations make no modification to the buffer. Therefore, in these cases, it is safe to simply retry the operation. This results in a busy-wait which is likely to succeed in a few attempts.

allocate/publish and peek/consume operations must always be performed in pairs and always in the correct order. Thus, if there are multiple readers or multiple writers, they must be synchronizied amongst each other, but writers do not have to be synchronized with readers and vice versa.

Also note that AtomicRingBuffer is intended to be used together with memcpy(). Therefore, AtomicRingBuffer will only provide pointers to consecutive blocks of memory. As AtomicRingBuffer also requires the user to make use of all bytes in the buffer, this means that an allocation or a peek may be partially fulfilled. In this case, the user must manage the fragmentation of the data across the wrap-around point of the buffer.

Usage

Create an AtomicRingBuffer object and provide it with a pointer and size to a uint8_t buffer of your chosing.

You can allocate Buffer space using AtomicRingBuffer::allocate() and then write data to the allocated memory. Once the write completes, the data must be published using AtomicRingBuffer::publish(). As soon as the data has been published, it is available to the reader.

A reader first uses AtomicRingBuffer::peek() to obtain a pointer to published data for reading of said data. Once the reader finishes processing the data, it must call AtomicRingBuffer::consume() to free the space in the buffer for further use by the writer.

Windows CI Linux CI

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.