Comments (4)
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.
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.
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.
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)
- Circular Buffer force_enque HOT 5
- some questions HOT 3
- Queue as member variable breaks my program HOT 2
- Please add to README how to run tests HOT 1
- On an M1 on MacOS, when profiling with Tracy, ReadWriterQueue's try_enqueue / try_dequeue sometimes seem to cause spikes of several milliseconds HOT 6
- Deadlock with readerwriterqueue when spinning over try_enqueue() method HOT 1
- peek() method for BlockingReaderWriterCircularBuffer? HOT 4
- Circular Buffer Doesn't Overwrite Old Data HOT 2
- Help with profiling OSX M1 HOT 17
- Any plan on latest release tag HOT 2
- Memory allocation HOT 2
- Upload stable release archive HOT 1
- same content poped when queue should be empty HOT 1
- Is Runtime Allocation Possible HOT 2
- Supported platforms/operating systems? HOT 3
- Assertion `!inSection && "Concurrent (or re-entrant) enqueue or dequeue operation detected (only one thread at a time may hold the producer or consumer role) HOT 1
- Segfault when attempting to peek() into the queue std::__atomic_base<bool>::load (__m=std::memory_order_relaxed, HOT 2
- Folly's LockFreeRingBuffer HOT 2
- can i use readwritequeue? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from readerwriterqueue.