justinstenning / SharedMemory

C# shared memory classes for sharing data between processes (Array, Buffer and Circular Buffer)
https://www.nuget.org/packages/SharedMemory
Other
566 stars 118 forks source link

Enable overwrite in CircularBuffer #39

Open abirmkj opened 5 years ago

abirmkj commented 5 years ago

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 commented 5 years ago

@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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

@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 commented 4 years ago

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