How to read data from a file in a multithreading loop

It looks like you are trying to the add variables to the same sum in the loop. You should calculate the parts of the sum for each bit of the loop, and then sum them afterwards instead.

Also, you should put your code inside a function instead of the global outer scope, and possibly consider chunking the code into smaller functions as this usually helps performance (due to type inference being a bit easier).

1 Like