进程之间共享数据:C# SharedMemory
jopen
10年前
C# shared memory classes 用于进程之间共享数据 (Array, Buffer and Circular Buffer)。封装了 .NET 4 的 MemoryMappedFile 用于实现快速低级的 IPC 进程间通讯。特别是用于进程间的数据共享。
提供的类库包括
- SharedMemory.Buffer - an abstract base class that wraps the .NET 4 MemoryMappedFile class, exposing the read/write operations and implementing a small header to allow clients to open the shared buffer without knowing the size beforehand.
- SharedMemory.BufferWithLocks - an abstract class that extends SharedMemory.Buffer to provide simple read/write locking support through the use of EventWaitHandles.
- SharedMemory.Array - a simple generic array implementation utilising a shared memory buffer. Inherits from SharedMemory.BufferWithLocks to provide support for thread synchronisation.
- SharedMemory.BufferReadWrite - provides read/write access to a shared memory buffer, with various overloads to support reading and writing structures, copying to and from IntPtr and so on. Inherits from SharedMemory.BufferWithLocks to provide support for thread synchronisation.
- SharedMemory.CircularBuffer - lock-free FIFO circular buffer implementation (aka ring buffer). Supporting 2 or more nodes, this implementation supports multiple readers and writers. The lock-free approach is implemented using Interlocked.Exchange and EventWaitHandles as explained here.