如何在并行嵌套循环上添加完成百分比

本文关键字:添加 百分比 嵌套循环 并行 | 更新日期: 2023-09-27 18:09:57

例如,我希望我能看到1% ~ 100%,我怎么能做到呢?

Parallel.For<Dictionary<int, long>>(0, r1, () => new Dictionary<int, long>(), (j, loop, tmpWinRange) =>
    {
        for (int k = 0; k < r2; k++)
            for (int l = 0; l < r3; l++)
                for (int m = 0; m < r4; m++)
                {
                    int pay = GetPay(j, k, l, m);
                    tmpWinRange[pay]++;
                }
        return tmpWinRange;
    },
(x) => { tmpWinRangeCollection.Add(x); }
);

如何在并行嵌套循环上添加完成百分比

如何使用联锁增量来更新进度?

int progressCount = 0;
Parallel.For<Dictionary<int, long>>(0, r1, () => new Dictionary<int, long>(), 
    (j, loop, tmpWinRange) => 
    { 
        for (int k = 0; k < r2; k++) 
            for (int l = 0; l < r3; l++) 
                for (int m = 0; m < r4; m++) 
                { 
                    int pay = GetPay(j, k, l, m); 
                    tmpWinRange[pay]++; 
                }
        Interlocked.Increment(ref progressCount);
        return tmpWinRange; 
    }, 
    (x) => { tmpWinRangeCollection.Add(x); } 
);