Created
August 23, 2015 13:13
-
-
Save angelovstanton/2952ad0b165c688b538f 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; | |
// Use type parameter to make subtotal a long, not an int | |
Parallel.For<long>(0, nums.Length, () => 0, (j, loop, subtotal) => | |
{ | |
subtotal += nums[j]; | |
return subtotal; | |
}, | |
(x) => Interlocked.Add(ref total, x) | |
); | |
Console.WriteLine("The total is {0:N0}", total); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment