Sergei Krainyukov
Sergei Krainyukov

Reputation: 87

Can a race condition occur when executing such code on Julia?

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

Upvotes: 3

Views: 189

Answers (1)

Simen Gaure
Simen Gaure

Reputation: 561

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.

Upvotes: 3

Related Questions