# Threads not working in julia 1.8.0

**URL:** https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083
**Category:** Numerics
**Created:** [August 21, 2022, 8:28am UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083 "2022-08-21T08:28:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mastrof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mastrof/32/17443_2.png) [@mastrof](https://discourse.julialang.org/u/mastrof)
#### Post date: [August 21, 2022, 8:28am UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083/1 "2022-08-21T08:28:27Z")

</div>

Just updated to 1.8.0 on my laptop and found out multithreading is not working as expected: loops never leave master thread

The basic example code behaves differently on 1.8.0 compared to my 1.6.2 installation

```julia
a = zeros(10)
Threads.@threads for i in 1:10
    a[i] = Threads.threadid()
end

```

On 1.6.2:

```julia
julia> a
10-element Vector{Float64}:
 1.0
 1.0
 2.0
 2.0
 3.0
 3.0
 4.0
 4.0
 5.0
 6.0

```

On 1.8.0:

```julia
julia> a
10-element Vector{Float64}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0

```

This is my setup on 1.8.0:

```julia
julia> versioninfo()
Julia Version 1.8.0
Commit 5544a0fab76 (2022-08-17 13:38 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 6 on 8 virtual cores
Environment:
  JULIA_COPY_STACKS = 1
  JULIA_NUM_THREADS = 6

```

```julia
(@v1.8) pkg> status
Status `~/.julia/environments/v1.8/Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.1
  [5fb14364] OhMyREPL v0.5.12
  [91a5bcdd] Plots v1.31.7
⌃ [d330b81b] PyPlot v2.10.0
  [295af30f] Revise v3.4.0

```

Any idea what’s happening?

EDIT: sorry I meant to post this in the general usage section; not sure how it ended up in the numerics

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [August 21, 2022, 8:32am UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083/2 "2022-08-21T08:32:00Z")

</div>

Are you sure you enabled multiple threads when starting Julia? Example: `julia --threads=4`.

See [Multi-Threading · The Julia Language](https://docs.julialang.org/en/v1/manual/multi-threading/)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 21, 2022, 8:38am UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083/3 "2022-08-21T08:38:40Z")

</div>

I cannot reproduce your problem. I get, after starting julia with

```julia
julia -t auto

```

```julia
julia> a
10-element Vector{Float64}:
 1.0
 1.0
 2.0
 2.0
 3.0
 4.0
 7.0
 8.0
 6.0
 5.0

```

---

<div class="post-metadata">

### Author: ![mastrof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mastrof/32/17443_2.png) [@mastrof](https://discourse.julialang.org/u/mastrof)
#### Post date: [August 21, 2022, 8:45am UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083/4 "2022-08-21T08:45:52Z")

</div>

I set the number of threads by the env variable `JULIA_NUM_THREADS` and it is correctly read by the repl as you see in the `versioninfo()` report. Also calling `Threads.nthreads()` returns 6 in my case.  
Starting with `-t auto` or other numbers changes the number of threads but does not change the results of the code.  
What could cause this? I stress that if I make two exactly identical environments in 1.8.0 and 1.6.2, in 1.6.2 I get the correct behavior.  
Should I just go for a full reinstallation of 1.8.0?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [August 21, 2022, 8:56am UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083/5 "2022-08-21T08:56:20Z")

</div>

> [@mastrof](#):
>
> ```julia
> Environment:
> JULIA_COPY_STACKS = 1
> 
> ```

I can reproduce your result if and only if I set this environment variable. I’m not familiar with what it does but I would start investigating in that end.

---

<div class="post-metadata">

### Author: ![mastrof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mastrof/32/17443_2.png) [@mastrof](https://discourse.julialang.org/u/mastrof)
#### Post date: [August 21, 2022, 9:06am UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083/6 "2022-08-21T09:06:53Z")

</div>

Oh damn. I’m honestly not sure what it does exactly but it is (was?) required to make `JavaCall` work; or at least it was at some point.  
Thanks for catching it, I had it sitting there for a few years and it never interfered with threading in the past versions.

Removing that variable solves the threading issue. Might need to open an issue for this

---

<div class="post-metadata">

### Author: ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)
#### Post date: [August 22, 2022, 7:33pm UTC](https://discourse.julialang.org/t/threads-not-working-in-julia-1-8-0/86083/7 "2022-08-22T19:33:57Z")

</div>

A PR was opened for this in March:

[@threads does not work with JULIA\_COPY\_STACKS=1 #44589](https://github.com/JuliaLang/julia/issues/44589)
