Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable overwrite in CircularBuffer #39

Open
abirmkj opened this issue Feb 5, 2019 · 5 comments
Open

Enable overwrite in CircularBuffer #39

abirmkj opened this issue Feb 5, 2019 · 5 comments

Comments

@abirmkj
Copy link

abirmkj commented Feb 5, 2019

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.

@justinstenning
Copy link
Owner

@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.

@cocowalla
Copy link

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
Copy link

@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?

@justinstenning
Copy link
Owner

@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.

@cocowalla
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants