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 9280287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:50:03+00:00 2026-06-18T17:50:03+00:00

I am searching for a ringbuffer-implementation in C in userspace, so I can use

  • 0

I am searching for a ringbuffer-implementation in C in userspace, so I can use it in my library.

Because I need a ringbuffer with

  • non-blocked write (=overwrite oldest data)
  • blocked read if empty

I searched a while and remembered I have used wait_event_interruptible & wake_up_interruptible to do something like this in kernel-mode.

But what is used in user-space so I maybe can search for a ringbuffer in combination with that method? I don’t want to re-invent the wheel – there are many ringbuffer-solutions around.

Thanks in advance & with kind regards!

EDIT:

It seems that maybe pthread_cond_wait could be an equivalent of wait_event_interruptible.

  • 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-18T17:50:04+00:00Added an answer on June 18, 2026 at 5:50 pm

    Adding another answer with some code, which isn’t 1:1 match to pseudocde in my other answer. Marking this as wiki answer, in case someone want’s to add comments or do other improvements. C phtread mutex+condition variable implementation of very simple ringbuffer:

    #include <stdio.h>
    #include <pthread.h>
    
    #define RINGBUFFER_SIZE (5)
    int ringbuffer[RINGBUFFER_SIZE];
    unsigned reader_unread = 0;
    unsigned writer_next = 0;
    pthread_mutex_t ringbuffer_mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t ringbuffer_written_cond = PTHREAD_COND_INITIALIZER;
    
    void process_code(int ch) {
        int counter;
        printf("Processing code %d", ch);
        for(counter=5; counter>0; --counter) {
            putchar('.');
            fflush(stdout);
            sleep(1);
        }
        printf("done.\n");
    
    }
    
    void *reader() {
        pthread_mutex_lock(&ringbuffer_mutex);
        for(;;) {
            if (reader_unread == 0) {
                pthread_cond_wait(&ringbuffer_written_cond, &ringbuffer_mutex);
            }
            if (reader_unread > 0) {
    
                int ch;
                int pos = writer_next - reader_unread;
                if (pos < 0) pos += RINGBUFFER_SIZE;
                ch = ringbuffer[pos];
                --reader_unread;
    
                if (ch == EOF) break;
    
                pthread_mutex_unlock(&ringbuffer_mutex);
                process_code(ch);
                pthread_mutex_lock(&ringbuffer_mutex);
            }
        }
        pthread_mutex_unlock(&ringbuffer_mutex);
    
        puts("READER THREAD GOT EOF");
        return NULL;
    }
    
    void *writer() {
        int ch;
        do {
            int overflow = 0;
            ch = getchar();
    
            pthread_mutex_lock(&ringbuffer_mutex);
    
            ringbuffer[writer_next] = ch;
    
            ++writer_next;
            if (writer_next == RINGBUFFER_SIZE) writer_next = 0;
    
            if (reader_unread < RINGBUFFER_SIZE) ++reader_unread;
            else overflow = 1;
    
            pthread_cond_signal(&ringbuffer_written_cond);
            pthread_mutex_unlock(&ringbuffer_mutex);
    
            if (overflow) puts("WARNING: OVERFLOW!");
    
        } while(ch != EOF);
    
        puts("WRITER THREAD GOT EOF");
        return NULL;
    }
    
    int main(void)
    {
        pthread_t reader_thread, writer_thread;
    
        puts("Starting threads. Type text and press enter, or type ctrl-d at empty line to quit.");
        pthread_create(&reader_thread, NULL, reader, NULL);
        pthread_create(&writer_thread, NULL, writer, NULL);
    
        pthread_join(writer_thread, NULL);
        pthread_join(reader_thread, NULL);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Searching did not find a similar question, so: How can POST data used by
Searching for a script, which can do show/hide functions without framework. Something like: <span
Searching in the second value of a map i use somthing like the following:
Searching the web for tutorials on how to use J9 as a JRE within
While searching for an implementation of the Barnsley's Fern fractal I came across a
Searching the 'net, I found that I should use :include , but that does
Searching turns up a simple definition: data hiding. But, consider the following two examples:
Searching for days but can’t find the solution. I saw some stream questions but
In searching I found that we can do something with rowStateVar or rowIndex attributes
After searching seems like the only SQL database that is free and can be

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.