Giter Site home page Giter Site logo

Comments (4)

cjappl avatar cjappl commented on June 3, 2024

Just had some rubber duck debugging here explaining the problem to you.

This seems to be working:

Setup:


struct StringBuffer
{
    char mBuffer[1024];
};

moodycamel::ReaderWriterQueue<StringBuffer> mStringBufferQueue {100};

Producer thread:

void ProduceResults()
{
    StringBuffer stringBuffer;
    const char *myString = "hello world!";
    strncpy(stringBuffer.mBuffer, myString, 1024);

    const bool success = mPimpl->mStringBufferQueue.try_enqueue(std::move(stringBuffer));
    assert(success); // If this fails, the queue may be too small
}

consuming thread

void Consume()
{
    StringBuffer stringBuffer;
    const bool foundAnItem = mPimpl->mStringBufferQueue.try_dequeue(stringBuffer);
    if (foundAnItem)
    {
        printf("CAPPLE StringBuffer: %s\n", stringBuffer.mBuffer);
    }
}

Going to leave this up over the weekend while I chew it over a bit more. Anyone and everyone feel free to poke holes in my plans if you feel the urge 😄

Thanks for such a great library, it's coming in extremely handy

from readerwriterqueue.

cameron314 avatar cameron314 commented on June 3, 2024

Yeah, was about to post a similar proof of concept:

#include <cstring>
#include <cassert>

struct FixedString {
    char s[128];
};

int main() {
    moodycamel::ReaderWriterQueue<FixedString> q{100};
    q.enqueue(FixedString{ "test123" });
    
    FixedString fs;
    q.try_dequeue(fs);
    assert(strcmp(fs.s, "test123") == 0);
}

from readerwriterqueue.

cameron314 avatar cameron314 commented on June 3, 2024

Note that this approach works but involves several copies along the way. Probably better would be to have an efficient allocator of 1024-byte blocks, and pass pointers to them around instead.

For logging specifically, there's usually some formatting (sprintf or ostream) going on when the message is being created, which implies there's already a temporary buffer. Allocating that memory from a pool then moving ownership of it into the queue will result in a minimal number of copies.

Depending on your needs, the "allocator" can be as simple as another queue in the other direction (log producer dequeues pointer to string buffer, writes to it, enqueues pointer to log queue; log consumer dequeues pointer to string buffer, writes it to file, then enqueues pointer to "free" block back onto the first queue).

from readerwriterqueue.

cjappl avatar cjappl commented on June 3, 2024

Depending on your needs, the "allocator" can be as simple as another queue in the other direction (log producer dequeues pointer to string buffer, writes to it, enqueues pointer to log queue; log consumer dequeues pointer to string buffer, writes it to file, then enqueues pointer to "free" block back onto the first queue).

Wow! Very clever idea....

I think that just may work for what I'm trying to do.

I really appreciate you weighing in, I was feeling very stuck with the problem! I am going to close this, but I will update my progress in this ticket in case someone else has this same question in the future. There is remarkably little content on the web about realtime safe loggers out there!

Thanks again

from readerwriterqueue.

Related Issues (20)

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.