# Question about data races

**URL:** <https://discourse.julialang.org/t/question-about-data-races/135037>\
**Category:** New to Julia\
**Tags:** parallel, multithreading\
**Created:** [January 14, 2026, 1:33am UTC](https://discourse.julialang.org/t/question-about-data-races/135037 "2026-01-14T01:33:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BananaMaster3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananamaster3/32/218457_2.png) [@BananaMaster3](https://discourse.julialang.org/u/BananaMaster3)\
**Post date:** [January 14, 2026, 1:33am UTC](https://discourse.julialang.org/t/question-about-data-races/135037/1 "2026-01-14T01:33:38Z")

</div>

Say we have a struct Foo:

```julia-auto
mutable struct Foo
   dee::Int
   fee::Int = 3
end

```

We then define an array of Foo’s

```julia-auto
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:

```julia-auto
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:

```julia-auto
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.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 14, 2026, 2:25am UTC](https://discourse.julialang.org/t/question-about-data-races/135037/2 "2026-01-14T02:25:21Z")

</div>

few things here:

```julia-auto
   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

```julia-auto
@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`
