public void ForInt32LocalSum(int from, int to) { var total = new AtomicInteger(0); var result = Parallel.For( from, to, () => 0, (i, s, l) => l + i, l => total.AddDeltaAndReturnNewValue(l)); Assert.That(result.IsCompleted, Is.True); Assert.That(total.Value, Is.EqualTo((from + to - 1) * (to - from) / 2)); }
public void ForInt32LocalSumWithParallelism(int from, int to, int parallelism) { var options = new ParallelOptions { MaxDegreeOfParallelism = parallelism }; var total = new AtomicInteger(0); var result = Parallel.For( from, to, options, () => 0, (i, s, l) => l + i, l => total.AddDeltaAndReturnNewValue(l)); Assert.That(result.IsCompleted, Is.True); Assert.That(total.Value, Is.EqualTo((from + to - 1) * (to - from) / 2)); }