# Problem understanding multi-threading

**URL:** <https://discourse.julialang.org/t/problem-understanding-multi-threading/45202>\
**Category:** New to Julia\
**Created:** [August 19, 2020, 10:02am UTC](https://discourse.julialang.org/t/problem-understanding-multi-threading/45202 "2020-08-19T10:02:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_de\_montserrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_de_montserrat/32/9135_2.png) [@Albert\_de\_montserrat](https://discourse.julialang.org/u/Albert_de_montserrat)\
**Post date:** [August 19, 2020, 10:02am UTC](https://discourse.julialang.org/t/problem-understanding-multi-threading/45202/1 "2020-08-19T10:02:06Z")

</div>

I fail to see why the following peace of code does not show the appropriate results. For every i-th iteration, the values of A B C D are correct, but the matrix FF sometimes contains the values that do not match the values of A B C D at the current iteration.

```
n1,n2 = (Int(2),Int(2))
a = rand(n1,n2)
b = rand(n1,n2)
c = rand(n1,n2)
d = rand(n1,n2)
function main(a,b,c,d)

n1 = size(a,1)
n2 = size(a,2)
FF = Array{Float32}(undef,2,2)    
aa = vec(a)
bb = vec(b)
cc = vec(c)
dd = vec(d)

Threads.@threads for ii = 1:n1*n2
    
        A = aa[ii]
        B = bb[ii]
        C = cc[ii]
        D = dd[ii]     
        FF .= [A B; C D]       

        println("i=$ii |--> A=$A, B=$B, C=$C, D=$D")  
        println("i=$ii |--> FF = $FF")

end

```

end

For example, I get:

```
i=1 |--> A=0.24456115815578272, B=0.8907566253514738, C=0.8642909161063068, 
D=0.42008839717317614
i=1 |--> FF = Float32[0.18490697 0.8512475; 0.00025189313 0.5059466]
i=2 |--> A=0.5474336567786457, B=0.07123365660482706, C=0.7875180836356128, 
D=0.4656796174911506
i=2 |--> FF = Float32[0.5474337 0.07123366; 0.7875181 0.46567962]
i=3 |--> A=0.1849069789564115, B=0.8512474692616838, C=0.00025189313264162294, 
D=0.5059465926785536
i=3 |--> FF = Float32[0.5474337 0.07123366; 0.7875181 0.46567962]
i=4 |--> A=0.22001676024179484, B=0.40712367021744145, C=0.21883641119645225, 
D=0.5583455220203384
i=4 |--> FF = Float32[0.22001676 0.40712368; 0.21883641 0.5583455]

```

where the results at i =1,3 do not match.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [August 19, 2020, 10:56am UTC](https://discourse.julialang.org/t/problem-understanding-multi-threading/45202/2 "2020-08-19T10:56:29Z")

</div>

This doesn’t look unexpected to me. Each thread overwrites all elements of `FF`, and then a bit later, reads these elements to make the string. Quite often, another thread’s writing will happen in between these two.

To safely write into an array from inside `Threads.@threads`, you need to ensure different threads never write to the same place. If you only write to `FF[ii]` then that’s sure to be safe. Sometimes it’s useful to explicitly write to `A[Threads.threadid()]`.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 19, 2020, 11:37am UTC](https://discourse.julialang.org/t/problem-understanding-multi-threading/45202/3 "2020-08-19T11:37:04Z")

</div>

> [@Albert\_de\_montserrat](#):
>
> where the results at i =1,3 do not match.

What @mcabbott said. Actually, I am surprised. Why would you expect them to be the same where several threads are writing to the same memory at the same time? And the order in which that can happen is non determinstic. Perhaps you are thinking the threads run one after the other, as long as you know that `@threads` runs things in parallel, it’s logical to expect that that will happen. Right?

---

<div class="post-metadata">

**Author:** ![Albert\_de\_montserrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_de_montserrat/32/9135_2.png) [@Albert\_de\_montserrat](https://discourse.julialang.org/u/Albert_de_montserrat)\
**Post date:** [August 19, 2020, 12:02pm UTC](https://discourse.julialang.org/t/problem-understanding-multi-threading/45202/4 "2020-08-19T12:02:50Z")

</div>

My understanding of parallel computing and Julia itself is still quite low so far. I had a very similar piece of code running well on a MATLAB’s parfor, I just assumed it would work in a similar way (I believe every thread/core gets a copy of the whole loop and variables?).

I think I’ve managed to solve the problem following @mcabbott suggestion, by defining FF as

```
FF = Array{Float32}(undef,2,2,nt)

```

and populating it in the loop as

```
FF[:,:,Threads.threadid()] .= [aa[ii] bb[ii]; cc[ii] dd[ii]]

```
