From a42b3393d4f20529a0c0ea7599dd66706b953faa Mon Sep 17 00:00:00 2001 From: Matthias Heil Date: Thu, 19 Nov 2026 09:50:21 +0100 Subject: [PATCH] Add typed shared-memory buffer example Introduce a generic shared buffer with typed span access and timeout-based read/write waits. Update the sample to verify data shared between two buffer views. --- ReaderApp/Program.cs | 3 +- WriterApp/Program.cs | 20 +++++++++--- WriterApp/SharedBuffer.cs | 65 ++++++++++++++++++++++++++++++++++++++ WriterApp/WriterApp.csproj | 3 +- 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 WriterApp/SharedBuffer.cs diff --git a/ReaderApp/Program.cs b/ReaderApp/Program.cs index b352f2d..d442ad7 100644 --- a/ReaderApp/Program.cs +++ b/ReaderApp/Program.cs @@ -1,4 +1,5 @@ -using SharedMemory; + +using SharedMemory; try { diff --git a/WriterApp/Program.cs b/WriterApp/Program.cs index 5c3c63a..421b9f2 100644 --- a/WriterApp/Program.cs +++ b/WriterApp/Program.cs @@ -1,8 +1,18 @@ -using SharedMemory; +using DotNext; +using System.Diagnostics; -using var numbers = new SharedArray("MySharedArray", length: 10); -numbers[0] = 123; -numbers[^1] = 456; +Console.WriteLine("WriterApp.SharedBuffer:"); +using var producer = new WriterApp.SharedBuffer("MySharedBuffer", length: 10); +var intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; +producer.Write(intArray); +using var consumer = new WriterApp.SharedBuffer("MySharedBuffer"); +var intSpan = consumer.DataSpan; +for (var i = 0; i < intSpan.Length; i++) +{ + Debug.Assert(intSpan[i] == intArray[i]); + +} Console.WriteLine("Start the second program, then press Enter here to close the array"); -Console.ReadLine(); \ No newline at end of file +Console.ReadLine(); + diff --git a/WriterApp/SharedBuffer.cs b/WriterApp/SharedBuffer.cs new file mode 100644 index 0000000..4959ddb --- /dev/null +++ b/WriterApp/SharedBuffer.cs @@ -0,0 +1,65 @@ +using DotNext.IO.MemoryMappedFiles; +using SharedMemory; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + + +namespace WriterApp; + +/// +/// A that creates no events, so that the lifetime of the shared memory +/// can be tested apart from the lifetime of named events. Compiled into the tests and the peer. +/// +public class SharedBuffer : BufferReadWrite where T : struct +{ + public SharedBuffer(string name, int length) : base(name, length * Unsafe.SizeOf()) + { + } + public SharedBuffer(string name) : base(name) + { + } + /// + /// Prevents write operations from deadlocking by throwing a TimeoutException if the WriteWaitEvent is not available within milliseconds + /// + private void WriteWait() + { + if (!WriteWaitEvent.WaitOne(ReadWriteTimeout)) + throw new TimeoutException("The write operation timed out waiting for the write lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock."); + } + public void Write(T[] array) + { + WriteWait(); + var span = DataSpan; + array.AsSpan().CopyTo(span); + } + /// + /// Prevents read operations from deadlocking by throwing a TimeoutException if the ReadWaitEvent is not available within milliseconds + /// + private void ReadWait() + { + if (!ReadWaitEvent.WaitOne(ReadWriteTimeout)) + throw new TimeoutException("The read operation timed out waiting for the read lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock."); + } + public void Read(T[] array) + { + ReadWait(); + var span = DataSpan; + span.CopyTo(array.AsSpan()); + } + public Span ByteSpan + { + get + { + return Mmf.CreateDirectAccessor(BufferOffset, BufferSize).Bytes; + } + } + public Span DataSpan + { + get + { + var spanT = MemoryMarshal.Cast(ByteSpan); + return spanT; + } + } + public T First => DataSpan[0]; +} diff --git a/WriterApp/WriterApp.csproj b/WriterApp/WriterApp.csproj index 1748380..90a9d38 100644 --- a/WriterApp/WriterApp.csproj +++ b/WriterApp/WriterApp.csproj @@ -8,7 +8,8 @@ - + +