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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:14:57+00:00 2026-05-14T18:14:57+00:00

I have some project where I have a single producer thread which writes events

  • 0

I have some project where I have a single producer thread which writes events into a buffer, and an additional single consumer thread which takes events from the buffer. My goal is to optimize this thing for a single dual-core machine to achieve maximum throughput.

Currently, I am using some simple lock-free ring buffer (lock-free is possible since I have only one consumer and one producer thread and therefore the pointers are only updated by a single thread).

#define BUF_SIZE 32768

struct buf_t { volatile int writepos; volatile void * buffer[BUF_SIZE]; 
    volatile int readpos;) };

void produce (buf_t *b, void * e) {
    int next = (b->writepos+1) % BUF_SIZE;
    while (b->readpos == next); // queue is full. wait
    b->buffer[b->writepos] = e; b->writepos = next;
}

void * consume (buf_t *b) {
    while (b->readpos == b->writepos); // nothing to consume. wait
    int next = (b->readpos+1) % BUF_SIZE;
    void * res = b->buffer[b->readpos]; b->readpos = next;
    return res;
}

buf_t *alloc () {
    buf_t *b = (buf_t *)malloc(sizeof(buf_t));
    b->writepos = 0; b->readpos = 0; return b;
}

However, this implementation is not yet fast enough and should be optimized further. I’ve tried with different BUF_SIZE values and got some speed-up. Additionaly, I’ve moved writepos before the buffer and readpos after the buffer to ensure that both variables are on different cache lines which resulted also in some speed.

What I need is a speedup of about 400 %. Do you have any ideas how I could achieve this using things like padding etc?

  • 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-05-14T18:14:58+00:00Added an answer on May 14, 2026 at 6:14 pm

    I’ve implemented the optimizations in the first code block of cafs answer. They actually gave some little speed up (thank you), however it is not yet enough. The second optimization which results in the cache being filled by column instead of by row results in a worse performance.

    The idea of the consumer lagging behind the producer did not gave any speedup.

    Now, I’m at 300%.

    An additional change I have made was some hack regarding the volatile writepos and readpos variables:

    void produce (void * e) {
        unsigned int oldpos = buffer.writepos;
        unsigned int next = (oldpos+1) % BUF_SIZE;
        while (next == buffer.rpos) { // rpos is not volatile
            buffer.rpos = buffer.readpos;
            usleep(1);
        }
        buffer.buffer[oldpos] = e; buffer.writepos = next;
    }
    

    and similar for the consume().

    Additional changes to the struct lead to the following new buffer struct (in the global scope as it was suggested in one answer instead of on the heap).

    #define STRIDE 16
    #define STEPS 524288
    
    struct buf_t {
        volatile unsigned int writepos;
        int offset [STRIDE - 1];
        unsigned int wpos;
        int offset2 [STRIDE - 1];
        volatile void * buffer[BUF_SIZE];
        int offset4 [STRIDE];
        volatile unsigned int readpos;
        int offset3 [STRIDE - 1];
        unsigned int rpos;
    }
    

    which gave the 300% speedup which was missing and pushed it below the performance limit I had to achieve.

    If you have some additional hacks which could be used to increase the performance further, don’t hesitate to post them also 🙂

    Thanks for help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have copied some files from project A to project B, both of which
Say I have some C++ project which builds an exe or dll file. The
I have some difficulties in my project. I got the RGB values from a
I have some local changes to an open source project which uses Subversion as
On a small embedded system project we have some code which we would like
I have some multi-module project. Some modules are packaged as wars and some as
I have some code in my project that saves an object to the database,
I have a project where there are multiple applications that have some common configuration
I have some strange linking problem in my Visual Studio 2005 C++ project. As
I have some interesting problem for an hour.. In my flex project, all width

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.