Question about data races

Say we have a struct Foo:

mutable struct Foo
   dee::Int
   fee::Int = 3
end

We then define an array of Foo’s

arrFoo = [Foo(1), Foo(1), Foo(2), Foo(3)]

Then we have a function that, when given foo and a specific item of Foo, modifies dee, but only looks at fee:

function modifyFoo(a::Foo, arr, index::Int)
   for t in arr
      a.dee += t.fee
   end
   arr[index] = a
end

Then we iterate over arrFoo, using multiple threads:

i = 1
Threads.@threads for item in arrFoo
   modifyFoo(item, arrFoo, i)
   i += 1
end

Does this situation create a data race? I don’t think it should, but I’m a bit nervous about it, considering that it is reading and writing to the same struct – just not the same items. Any comments clearing up how data races work are much appreciated.

few things here:

   for t in arr
      a.dee += t.fee
   end
   arr[index] = a

you have already obtained a as arr[index], so modifying the object already modifies in place. there’s no need to assign it back to the array via arr[index] = a

also, you do have a data race on i since you have a threaded loop modifying a non-atomic variable. you probably want that to be

@threads for (i, item) in collect(enumerate(arrFoo))
...
end

you will also want to ensure that all the elements of arrFoo are indeed distinct objects in memory; if two of them are actually the same object appearing twice in the array, then you will see a data race in assignment to .dee