# ‌Basic question about Threads.@threads and multithreading

**URL:** <https://discourse.julialang.org/t/basic-question-about-threads-threads-and-multithreading/54420>\
**Category:** New to Julia\
**Tags:** multithreading\
**Created:** [February 1, 2021, 9:35pm UTC](https://discourse.julialang.org/t/basic-question-about-threads-threads-and-multithreading/54420 "2021-02-01T21:35:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arshia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arshia/32/18748_2.png) [@Arshia](https://discourse.julialang.org/u/Arshia)\
**Post date:** [February 1, 2021, 9:35pm UTC](https://discourse.julialang.org/t/basic-question-about-threads-threads-and-multithreading/54420/1 "2021-02-01T21:35:13Z")

</div>

Hello everybody.

I’m very new to Julia. I read documents and many topics in the discourse still I’m not sure I’m doing right.  
I want to implement the 2 sets of equations in multi-threads.  
my equations something like that

#------First Set------  
1. **ex = ex + aE1 + aX**  
2. **ey = ey + bE2 + bX**  
3. **ey = ey + cE3 + cX**  
#------Second Set------  
4. **E1 = ex + E1**  
5. **E1 = ey + E1**  
6. **E1 = ez + E1**

I want to run 1,2,3 (at the same time), then 4,5,6, simultaneously.  
what I do in Julia:

```julia
function calculate_E(fields)
 Threads.@threads for i in 1:(c_x)
            for j in 2:(c_y)
                for k in 2:(c_z-1)
                      k_x[i,j,k] = k_x[i,j,k]+ a[i,j,k]*E1[i,j,k] + a[i,j,k]*X[i,j,k]
                end
            end
        end

 Threads.@threads for i in 2:(c_x-1)
            for j in 1:(c_y)
                for k in 2:(c_z)
                      k_y[i,j,k] = k_y[i,j,k]+ a[i,j,k]*E2[i,j,k] + a[i,j,k]*X[i,j,k]
                end
            end
        end

 Threads.@threads for i in 2:(c_x)
            for j in 2:(c_y-1)
                for k in 1:(c_z)
                      k_z[i,j,k] = k_z[i,j,k]+ a[i,j,k]*E3[i,j,k] + a[i,j,k]*X[i,j,k]
                end
            end
        end
end

function aux_field(fields)

        Threads.@threads for i in 1:(c_x)
            for j in 2:(c_y)
                for k in 2:(c_z)
                    E1[i,j,k] = k_x[i,j,k] + E1[i,j,k]
                end
            end
        end

        Threads.@threads for i in 2:(c_x)
            for j in 1:(c_y)
                for k in 2:(c_z)
                    E2[i,j,k] = k_y[i,j,k] + E2[i,j,k]
                end
            end
        end

        Threads.@threads for i in 2:(c_x)
            for j in 2:(c_y)
                for k in 1:(c_z)
                    E1[i,j,k] = k_z[i,j,k] + E1[i,j,k]
                end
            end
        end

end

   @time for time in 1:t
            k_x, k_y, k_z=calculate_ex(k_x, k_y, k_z, E1, E2, E3)

            E1, E2, E3=aux_field(k_x, k_y, k_z, E1, E2, E3)
    end

```

am I do it right? When I use Threads.@threads in those functions?

and another question in the function “ **aux\_field** ” in single threads I can simply write:

```julia
E1 = ex + E1
E1 = ey + E1
E1 = ez + E1

```

Is there a way to make them multithreading without for loops, and it’s even feasible?

P.S. my code’s answers are correct.

Thanks

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [February 2, 2021, 1:01am UTC](https://discourse.julialang.org/t/basic-question-about-threads-threads-and-multithreading/54420/2 "2021-02-02T01:01:45Z")

</div>

It’ll be easier for others to respond to your question if you can provide a minimal working example (including definitions for all your variables) that can be copy-pasted into the REPL. As it stands, I can’t guess the eltypes or dimensions of any of your variables, which makes it hard to provide any specific advice.
