简体   繁体   中英

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

If v is an Array there will be no race condition. Accessing different array elements in different threads is safe.

However, if v is eg a Dict{Int, Float64} you can have race conditions. Similarly, you are not guaranteed thread safety for subtypes of AbstractArray , like BitVector .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM