c# Parallel.For 并行编程 执行顺序测试
生活随笔
收集整理的這篇文章主要介紹了
c# Parallel.For 并行编程 执行顺序测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?因為有個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(0, 6 /*portion*/, (i, ParallelLoopState) =>{Console.WriteLine(la[i]) ;});結果:
順序大變
再運行一次
因為設置斷點,程序停了一會顯示按照先后了
?
?在運行一次,去掉斷點
總結:對需要先后順序的操作明顯滿足不了
本來已經加上一下代碼就可以了
if (i == 0){using (mmf_reader = mmf.CreateViewAccessor(0, portion1, MemoryMappedFileAccess.Read)){buffer = new byte[portion1];mmf_reader.ReadArray(0, buffer, 0, (int)portion1);mappedFiles.Add(new MappedFile{Offset =/* i **/ portion1, //fileOffset, Buffer = buffer,FileSize = portion1});}}else if (i == 1){using (mmf_reader = mmf.CreateViewAccessor(portion1, portion2 - portion1, MemoryMappedFileAccess.Read)){buffer = new byte[portion2 - portion1];mmf_reader.ReadArray(0, buffer, 0, (int)(portion2 - portion1));mappedFiles.Add(new MappedFile{Offset = portion1 + portion2,// i * portionSize, //fileOffset, Buffer = buffer,FileSize = portion2 - portion1});}}else if (i == 2){using (mmf_reader = mmf.CreateViewAccessor(portion2, portion3 - portion2, MemoryMappedFileAccess.Read)){buffer = new byte[portion3 - portion2];mmf_reader.ReadArray(0, buffer, 0, (int)(portion3 - portion2));mappedFiles.Add(new MappedFile{Offset = portion3 + portion2,// i * portionSize, //fileOffset, Buffer = buffer,FileSize = portion3 - portion2});}}else if (i == 3){using (mmf_reader = mmf.CreateViewAccessor(portion2, portion3 - portion2, MemoryMappedFileAccess.Read)){buffer = new byte[portion3 - portion2];mmf_reader.ReadArray(0, buffer, 0, (int)(portion3 - portion2));mappedFiles.Add(new MappedFile{Offset = portion3 + portion2,// i * portionSize, //fileOffset, Buffer = buffer,FileSize = portion3 - portion2});}}else if (i == 4){using (mmf_reader = mmf.CreateViewAccessor(portion3, portion4 - portion3, MemoryMappedFileAccess.Read)){buffer = new byte[portion4 - portion3];mmf_reader.ReadArray(0, buffer, 0, (int)(portion4 - portion3));mappedFiles.Add(new MappedFile{Offset = portion3 + portion2,// i * portionSize, //fileOffset, Buffer = buffer,FileSize = portion4 - portion3});}}else if (i == 5){using (mmf_reader = mmf.CreateViewAccessor(portion4, portion5 - portion4, MemoryMappedFileAccess.Read)){buffer = new byte[portion5 - portion4];mmf_reader.ReadArray(0, buffer, 0, (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 = 0; i < 6; i++){MappedFile map = new MappedFile();mappedFiles.Add(map); mappedFiles[0]=new MappedFile { // Offset =/* i **/ portion1, //fileOffset, Buffer = buffer, // FileSize = portion1 };
?
不用list.add的方式,直接賦值方式.問題解決
?轉載于:https://www.cnblogs.com/zuochanzi/p/7365632.html
總結
以上是生活随笔為你收集整理的c# Parallel.For 并行编程 执行顺序测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原创:六国知道秦国要一个个灭了他们,为什
- 下一篇: python基础===使用switch方