Read and Write Events
.OnRead and .OnWrite are events where post-read and write actions can be added. Read and write events are needed for cache implementations where it is necessary to know which resources are used frequently or lately.
long[] accessTicks = new long[2];
IMemory<byte> memory = new BlockMemory<byte>().RWLocked();
memory.OnRead += () => accessTicks[0] = DateTime.UtcNow.Ticks;
memory.OnWrite += () => accessTicks[1] = DateTime.UtcNow.Ticks;
// Write event
memory.Add(item: 100);
// Read event
byte value = memory[0];
// Print time
Console.WriteLine(accessTicks[0]);
Console.WriteLine(accessTicks[1]);
Full Example
Full example
using Avalanche.Memory;
using Avalanche.Utilities;
using static System.Console;
public class rwevent
{
public static void Run()
{
{
// <01>
long[] accessTicks = new long[2];
IMemory<byte> memory = new BlockMemory<byte>().RWLocked();
memory.OnRead += () => accessTicks[0] = DateTime.UtcNow.Ticks;
memory.OnWrite += () => accessTicks[1] = DateTime.UtcNow.Ticks;
// Write event
memory.Add(item: 100);
// Read event
byte value = memory[0];
// Print time
Console.WriteLine(accessTicks[0]);
Console.WriteLine(accessTicks[1]);
// </01>
}
}
}