Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:02:14+00:00 2026-06-10T20:02:14+00:00

I am looking for a ring buffer implementation (or pseudocode) in C with the

  • 0

I am looking for a ring buffer implementation (or pseudocode) in C with the following characteristics:

  • multiple producer single consumer pattern (MPSC)
  • consumer blocks on empty
  • producers block on full
  • lock-free (I expect high contention)

So far I’ve been working only with SPSC buffers – one per producer – but I would like to avoid the continuous spinning of the consumer to check for new data over all its input buffers (and maybe to get rid of some marshaling threads in my system).

I develop for Linux on Intel machines.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:02:15+00:00Added an answer on June 10, 2026 at 8:02 pm

    I think I have what you are looking for. It is a lock free ring buffer implementation that blocks producer/consumer. You only need access to atomic primitives – in this example I will use gcc’s sync functions.

    It has a known bug – if you overflow the buffer by more than 100% it is not guaranteed that the queue remains FIFO (it will still process them all eventually).

    This implementation relies on reading/writing the buffer elements as being an atomic operation (which is pretty much guaranteed for pointers)

    struct ringBuffer
    {
       void** buffer;
       uint64_t writePosition;
       size_t size;
       sem_t* semaphore;
    }
    
    //create the ring buffer
    struct ringBuffer* buf = calloc(1, sizeof(struct ringBuffer));
    buf->buffer = calloc(bufferSize, sizeof(void*));
    buf->size = bufferSize;
    buf->semaphore = malloc(sizeof(sem_t));
    sem_init(buf->semaphore, 0, 0);
    
    //producer
    void addToBuffer(void* newValue, struct ringBuffer* buf)
    {
       uint64_t writepos = __sync_fetch_and_add(&buf->writePosition, 1) % buf->size;
    
       //spin lock until buffer space available
       while(!__sync_bool_compare_and_swap(&(buf->buffer[writePosition]), NULL, newValue));
       sem_post(buf->semaphore);
    }    
    
    //consumer
    void processBuffer(struct ringBuffer* buf)
    {
       uint64_t readPos = 0;
       while(1)
       {
           sem_wait(buf->semaphore);
    
           //process buf->buffer[readPos % buf->size]
           buf->buffer[readPos % buf->size] = NULL;
           readPos++;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at the Ehcahce implementation of net.sf.cache.JS107, I am trying to achieve the following
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking for a perl one-liner what will find all words with the next pattern:
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
I'm looking for an example of a 'device ring', somewhat like the one used
Looking at the following code, does theme[sprite].img get nested inside result[definition].data (as theme[sprite].img is
I'm trying to simply read the kernel ring buffer from a kernel module. Also
Currently, I have a RingBuffer which is run by a producer and a consumer
Looking at some old code we have lots of things like the following: //
Looking at the missing index DMVs on SQLServer it suggests I add the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.