# Is it safe to run this code in multithread way: modify the same data frame in loop

**URL:** <https://discourse.julialang.org/t/is-it-safe-to-run-this-code-in-multithread-way-modify-the-same-data-frame-in-loop/64896>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [July 19, 2021, 2:17am UTC](https://discourse.julialang.org/t/is-it-safe-to-run-this-code-in-multithread-way-modify-the-same-data-frame-in-loop/64896 "2021-07-19T02:17:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [July 19, 2021, 2:17am UTC](https://discourse.julialang.org/t/is-it-safe-to-run-this-code-in-multithread-way-modify-the-same-data-frame-in-loop/64896/1 "2021-07-19T02:17:56Z")

</div>

I wonder, is it safe to modify the same Data Frame in a multithread loop?

> @threads for i in 1:size(df)[1]  
> df.column[i] = some\_function(…)  
> end

Thank you in advance.

Best

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 19, 2021, 4:52am UTC](https://discourse.julialang.org/t/is-it-safe-to-run-this-code-in-multithread-way-modify-the-same-data-frame-in-loop/64896/2 "2021-07-19T04:52:06Z")

</div>

Generally, yes. See [https://github.com/JuliaData/DataFrames.jl/issues/1905](https://github.com/JuliaData/DataFrames.jl/issues/1905)

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 19, 2021, 6:18am UTC](https://discourse.julialang.org/t/is-it-safe-to-run-this-code-in-multithread-way-modify-the-same-data-frame-in-loop/64896/3 "2021-07-19T06:18:30Z")

</div>

I think this would be super slow due to type instability (especially when `some_function` also depends on an element of `df`) - the type of `df.column` is not known at compile-time. Better broadcast your function over a complete column, this would not be multi-threaded but probably much faster overall.

---

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [July 20, 2021, 5:59pm UTC](https://discourse.julialang.org/t/is-it-safe-to-run-this-code-in-multithread-way-modify-the-same-data-frame-in-loop/64896/4 "2021-07-20T17:59:21Z")

</div>

Yes, you are right.  
I just wonder, about how is safe to modify the same dataframe in the different threads. (not for sure not the same row of the df)
