Skip to content

Instantly share code, notes, and snippets.

@msymt
Created October 17, 2022 08:47
Show Gist options
  • Save msymt/bb5e68da6e8a121cfcd04abfae3f0eb3 to your computer and use it in GitHub Desktop.
Save msymt/bb5e68da6e8a121cfcd04abfae3f0eb3 to your computer and use it in GitHub Desktop.
read 1byte and increment from MappedMemoryFile in C#
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
public class HelloWorld
{
static public void Main ()
{
string MappedMemoryFileName = "/tmp/mmf";
using (var mmf = MemoryMappedFile.CreateFromFile(MappedMemoryFileName, FileMode.OpenOrCreate, MappedMemoryFileName))
{
int cur_loc = 0;
int prev_loc = 0;
int pivot = Convert.ToInt32(cur_loc ^ prev_loc);
// start: pivot, size: 1
using (var accessor = mmf.CreateViewAccessor(pivot, 1))
{
// start position: 0
var result = accessor.ReadByte(0);
Console.WriteLine("NUMBER: " + result);
result += 1;
accessor.Write(0, ref result);
}
}
}
}