I want to convert this simple for loop into a parallel one. It loops over an array of strings (floats read from a text file) and computes the sum.
for (int i = 0; i < parts.Length; i++)
{
float tmp;
if (float.TryParse(parts[i], out tmp) || float.TryParse(parts[i].Replace('.', ','), out tmp))
total += tmp;
}
this is my try :
Parallel.For(0, parts.Length, i =>
{
float tmp;
if (float.TryParse(parts[i], out tmp) || float.TryParse(parts[i].Replace('.', ','), out tmp))
total += tmp;
});
It returns a float but the value is wrong.
I also tryed this one.
Parallel.For<float>(0, parts.Count, () => 0, (i, loop, subtotal) =>
{
total += result[i];
return subtotal;
},
(x) => Interlocked.Add(ref sum, x)
);
Couldn't get the syntax down.
Also I know it sounds ridiculous but most examples are with integers and eventually they use a method Add that only accepts int parameters, which I can't use obviously.