Why is your source code broken, check other answers, I will not repeat this.
Multithreading is easiest when there is no write access to the general state. Fortunately, your code can be written that way. Parallel linq can be nice in such situations, but sometimes there is too much overhead.
You can rewrite your code on:
double sqrt_min = myArr.AsParallel().Select(x=>Math.Sqrt(x)).Min();
In your specific task, it is faster to exchange Min and Sqrt , which is possible because Sqrt monotonically increasing.
double sqrt_min = Math.Sqrt(myArr.AsParallel().Min())
CodesInChaos
source share