SharedMemory icon indicating copy to clipboard operation
SharedMemory copied to clipboard

Enable overwrite in CircularBuffer

Open abirmkj opened this issue 6 years ago • 5 comments

Hi @spazzarama , first of all thanks for this excellent library. It's made my life easier while developing numerous IPC applications.

One of my current applications has a high-frequency data producer coupled with a lower-frequency data consumer. I would like the new data to overwrite older data regardless of whether it has been consumed. In other words, I need the latest data to be available at any given time.

How would I achieve this? I feel it may contradict #8 in some ways.

As a suggestion, in a non-IPC variant of the CircularBuffer, I had used a Boolean property to allow/disallow the producer overwriting the data.

abirmkj avatar Feb 05 '19 16:02 abirmkj

@abiresque in the meantime this could be implemented easily by checking if the result of the write succeeded (using a smaller timeout). If it fails, simply have a consumer read and discard an item and try to write again.

justinstenning avatar May 07 '19 09:05 justinstenning

Firstly, thanks for this library, it's great to have a simple abstraction over shared memory!

Just wanted to chime in with another voice in favour of built-in support for this (I'd assumed CircularBuffer worked like this, and wondered what was going on) 😄

cocowalla avatar May 28 '20 20:05 cocowalla

@spazzarama I'm thinking about your workaround, which I think is something like:

byte[] output = // some buffer here;

// Try to write to the buffer. If we fail, keep taking and discarding items until we succeed
while (buffer.Write(data) == 0)
{
    buffer.Read(output);
}

The problem is that the producer will be discarding the newest data, rather than the oldest. Is there some way to read from the oldest end of the circular buffer instead of the newest?

cocowalla avatar May 29 '20 10:05 cocowalla

@cocowalla are you sure it is discarding the newest? It is a FIFO buffer, and the read/write locations are stored in the shared memory circular buffer header.

BTW you can use the Read overload that takes an Action instead that skips reading the actual bytes to speed things up.

justinstenning avatar May 29 '20 11:05 justinstenning

Sorry, I got confused, you're right that it reads from the oldest data!

cocowalla avatar May 29 '20 11:05 cocowalla