因为有个for 实际执行结果尽管是按照for里边的顺序执行,但处理器让哪个分线程先执行,谁先处理完就不一定了.
对于要求结果需要先后顺序的,比如对text内容的操作, 用并行 Parallel.For 操作,不做进一步优化处理,那结果就不是想要的了,还要l用它的并行的功能所以要多程序进行改进,
我使用的做法是初始化buffer list 把list数组的顺序定下来,在循环中,把对应的值顺序赋值给list. 这样做可能写的有点死,根据实际应用调整启动循环的数量实际运行起来效果不错.
做了个简单的测试,同时赋值多个字符串,打印list字符串的值,
string a = "sssssssssssssssssssssssssssssssssssssssssss";
string b = "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj";
string c = "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnmmm";
string d = "ssssssssssssssssssssssssssssssssssssssssssssssssss";
string o = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";
string f = "ttttttttttttttttttttttttttttttttttttttttttttttttttt";
List<string> la = new List<string>();
la.Add(a);
la.Add(b);
la.Add(c);
la.Add(d);
la.Add(o);
la.Add(f);
Parallel.For(, /*portion*/, (i, ParallelLoopState) =>
{
Console.WriteLine(la[i]) ;
});
结果:
顺序大变
再运行一次
因为设置断点,程序停了一会显示按照先后了
在运行一次,去掉断点
总结:对需要先后顺序的操作明显满足不了
本来已经加上一下代码就可以了
if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(, portion1, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion1]; mmf_reader.ReadArray(, buffer, , (int)portion1);
mappedFiles.Add(new MappedFile
{
Offset =/* i **/ portion1, //fileOffset,
Buffer = buffer,
FileSize = portion1
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion1, portion2 - portion1, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion2 - portion1]; mmf_reader.ReadArray(, buffer, , (int)(portion2 - portion1));
mappedFiles.Add(new MappedFile
{
Offset = portion1 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion2 - portion1
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion2, portion3 - portion2, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion3 - portion2]; mmf_reader.ReadArray(, buffer, , (int)(portion3 - portion2));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion3 - portion2
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion2, portion3 - portion2, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion3 - portion2]; mmf_reader.ReadArray(, buffer, , (int)(portion3 - portion2));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion3 - portion2
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion3, portion4 - portion3, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion4 - portion3]; mmf_reader.ReadArray(, buffer, , (int)(portion4 - portion3));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion4 - portion3
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion4, portion5 - portion4, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion5 - portion4]; mmf_reader.ReadArray(, buffer, , (int)(portion5 - portion4));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion5 - portion4
});
}
}
}
以上尽管值加进去了,里边顺序还是变了并行线程用起来还是有些复杂度的,
对以上
mappedFiles 先初始化 ,
mappedFiles = new List<MappedFile>();
//初始化 list 六个
for (int i = ; i < ; i++)
{
MappedFile map = new MappedFile();
mappedFiles.Add(map);
mappedFiles[]=new MappedFile
{
// Offset =/* i **/ portion1, //fileOffset,
Buffer = buffer,
// FileSize = portion1
};
不用list.add的方式,直接赋值方式.问题解决