Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created August 23, 2015 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/2952ad0b165c688b538f to your computer and use it in GitHub Desktop.
Save angelovstanton/2952ad0b165c688b538f to your computer and use it in GitHub Desktop.
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