This multithreading behavior with a weird “fix” happens on 1.4.2 for me, but not 1.5 or 1.6. I don’t understand what I’m doing wrong, so I’m wary of just upgrading and forgetting about it:
module M
function f()
x = zeros(Int, 10000)
Threads.@threads for i in 1:length(x)
n = i
x[n] = n
end
n = sum(x) # weird behavior "fixed" if we change 'n' to
return n # some other name here and here
end
println(f()) # should print 50005000, but does not
end
But instead of printing 50005000 at the end, we get a random number each time we run. This is seemingly fixed by changing
n = sum(x)
return n
to
a = sum(x)
return a
Am I misunderstanding scoping for n
inside @threads
? Thank you!