次の 2 つのプロセスがあります。
- Win32、C++ - ライター
- .Net 4.5、C# - リーダー
最初のプロセスはバッファを作成し、2 番目のプロセスのために共有します。
- (int)(buffer+0) - 書き込めるまで。
- (int)(buffer+4) - 読み取り可能になるまで。
- ... - ブロック [size_mess][mess]
循環性を記録します。つまり、バッファの最後に到達したら、最初にシークします。
ある時点でエラーが発生します。
1 プロセスは、データが読み取られるのを待ちます。2 プロセスはブロックを読み取りますが、古いデータ (前のパスで記録されたもの) を読み取ります。
MemoryMappedViewAccessor、MemoryMappedViewStream を使用してみました...効果なし
.NET による遅延の可能性はありますか?
unsafe public void LoadFromMemory(string name)
{
const UInt32 capacity = 1200;
const UInt32 maxsize = 1024;
MemoryMappedFile mf = MemoryMappedFile.OpenExisting(name,MemoryMappedFileRights.FullControl);
MemoryMappedViewStream stream = mf.CreateViewStream(0, capacity,MemoryMappedFileAccess.ReadWrite);
BinaryReader reader = new BinaryReader(stream);
byte* bytePtr = null;
stream.SafeMemoryMappedViewHandle.AcquirePointer(ref bytePtr);
int size = 0;
long pos_begin = 0x10;
long pos_max = Interlocked.CompareExchange(ref *((int*)(bytePtr + 4)), 0, 0);
while (<work>)
{
while (pos_begin >= pos_max)
{
pos_max = Interlocked.CompareExchange(ref *((int*)(bytePtr+4)), 0, 0);
}
size = (bytePtr[pos_begin + 1] << 8) + bytePtr[pos_begin];
stream.Seek(pos_begin + 2, SeekOrigin.Begin);
work(reader);
//if here put a breakpoint,
//in time of error size ! = Watch.bytePtr[pos_begin] and all other data
if (pos_begin + size > maxsize) pos_begin = 0x10; // to beginning
else pos_begin += size;
Interlocked.Exchange(ref *((int*)bytePtr), (int)pos_begin); // for first process
}
}