给出以下测试代码(x64环境):
static void Test()
{
string fileName = @"d:\map";
long length = new FileInfo(fileName).Length;
using (var file = MemoryMappedFile.CreateFromFile(fileName, FileMode.Open, "mapFile", length, MemoryMappedFileAccess.ReadWrite))
{
byte* bytePtr = (byte*)0;
var view = file.CreateViewAccessor(0, length, MemoryMappedFileAccess.ReadWrite);
view.SafeMemoryMappedViewHandle.AcquirePointer(ref bytePtr);
long count = (long)(length / sizeof(int));
long sum = 0;
long step = count / 2000;
int* ptr = (int*)&bytePtr[0];
long currentCount = 0 ;
Parallel.For(0, count, new ParallelOptions { MaxDegreeOfParallelism = 8 }, (i) =>
{
Interlocked.Add(ref sum, ptr[i]);
Interlocked.Increment(ref currentCount) ;
if …Run Code Online (Sandbox Code Playgroud)