I have a function below. Can a race condition occur when executing such code?
function thread_test(v)
Threads.@threads for i = 1:length(v)
@inbounds v[i] = rand()
end
sum(v)
end
CodePudding user response:
If v
is an Array
there will be no race condition. Accessing different array elements in different threads is safe.
However, if v
is e.g. a Dict{Int, Float64}
you can have race conditions. Similarly, you are not guaranteed thread safety for subtypes of AbstractArray
, like BitVector
.