# Multithreading and for loops

**URL:** <https://discourse.julialang.org/t/multithreading-and-for-loops/31565>\
**Category:** New to Julia\
**Created:** [November 27, 2019, 8:44am UTC](https://discourse.julialang.org/t/multithreading-and-for-loops/31565 "2019-11-27T08:44:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 27, 2019, 8:44am UTC](https://discourse.julialang.org/t/multithreading-and-for-loops/31565/1 "2019-11-27T08:44:55Z")

</div>

Why does this works

```julia
using Base.Threads

A = zeros(3,3)

Threads.@threads for col = 1:3
    for row = 1:3
        A[row,col]=(col-1)*3+row
    end
end

display(A)

```

But this does not work

```julia
using Base.Threads

A = zeros(3,3)

Threads.@threads for col = 1:3, row = 1:3
        A[row,col]=(col-1)*3+row
end

display(A)

```

```julia
ERROR: LoadError: syntax: invalid assignment location "col = 1:3"
Stacktrace:
 [1] top-level scope at /Users/ssiew/juliascript/threads02.jl:5
in expression starting at /Users/ssiew/juliascript/threads02.jl:5

```

Even through the two are practically identical as far as I can tell.

And the non-multithreading version below works as well

```julia
using Base.Threads

A = zeros(3,3)

for col = 1:3, row = 1:3
        A[row,col]=(col-1)*3+row
end

display(A)

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 27, 2019, 9:34am UTC](https://discourse.julialang.org/t/multithreading-and-for-loops/31565/2 "2019-11-27T09:34:45Z")

</div>

The macro simply does not have support for loops with two variables, you must specify explicitly which loop you would like to thread by splitting the expression into two explicit loops.
