日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# Parallel.For 并行编程 执行顺序测试

發(fā)布時間:2025/3/8 C# 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# Parallel.For 并行编程 执行顺序测试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?因為有個for 實際執(zhí)行結(jié)果盡管是按照for里邊的順序執(zhí)行,但處理器讓哪個分線程先執(zhí)行,誰先處理完就不一定了.

對于要求結(jié)果需要先后順序的,比如對text內(nèi)容的操作, 用并行?Parallel.For 操作,不做進一步優(yōu)化處理,那結(jié)果就不是想要的了,還要l用它的并行的功能所以要多程序進行改進,

我使用的做法是初始化buffer list 把list數(shù)組的順序定下來,在循環(huán)中,把對應(yīng)的值順序賦值給list. ? ?這樣做可能寫的有點死,根據(jù)實際應(yīng)用調(diào)整啟動循環(huán)的數(shù)量實際運行起來效果不錯.

做了個簡單的測試,同時賦值多個字符串,打印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]) ;});

結(jié)果:

順序大變

再運行一次

因為設(shè)置斷點,程序停了一會顯示按照先后了

?

?在運行一次,去掉斷點

總結(jié):對需要先后順序的操作明顯滿足不了

本來已經(jīng)加上一下代碼就可以了

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});}}}

?以上盡管值加進去了,里邊順序還是變了并行線程用起來還是有些復(fù)雜度的,

對以上

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的方式,直接賦值方式.問題解決

?

轉(zhuǎn)載于:https://www.cnblogs.com/zuochanzi/p/7365632.html

總結(jié)

以上是生活随笔為你收集整理的c# Parallel.For 并行编程 执行顺序测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。