Created
August 23, 2015 13:13
-
-
Save angelovstanton/03cc00bd0c5fc031fe17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] nums = Enumerable.Range(0, 1000000).ToArray(); | |
long total = 0; | |
Parallel.ForEach<int, long>(nums, // source collection | |
() => 0, // method to initialize the local variable | |
(j, loop, subtotal) => // method invoked by the loop on each iteration | |
{ | |
subtotal += j; //modify local variable | |
return subtotal; // value to be passed to next iteration | |
}, | |
// Method to be executed when each partition has completed. | |
// finalResult is the final value of subtotal for a particular partition. | |
(finalResult) => Interlocked.Add(ref total, finalResult)); | |
Console.WriteLine("The total from Parallel.ForEach is {0:N0}", total); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment