# Multi-threading bug?

**URL:** https://discourse.julialang.org/t/multi-threading-bug/32937
**Category:** General Usage
**Tags:** multithreading
**Created:** [January 3, 2020, 7:09pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937 "2020-01-03T19:09:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [January 3, 2020, 7:09pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/1 "2020-01-03T19:09:11Z")

</div>

I am trying to make my code to run on multiple threads and I was reading through [this](https://julialang.org/blog/2019/07/multithreading). I got the following very weird result:

```julia
julia> Threads.nthreads()
6

julia> Threads.@threads for i = 1:10
             println("i = $i on thread $(Threads.threadid())")
           end
i = 1 on thread 1
i = 2 on thread 1
i = 3 on thread 1
i = 4 on thread 1
i = 5 on thread 1
i = 6 on thread 1
i = 7 on thread 1
i = 8 on thread 1
i = 9 on thread 1
i = 10 on thread 1

```

That is what I got when I run thing in juno. So I tried to do this from the terminal. I run `julia --proc 8` and it takes considerably more time to launch than when I just run `julia` but then I get:

```julia
julia> Threads.nthreads()
1

```

What is going on here?

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [January 3, 2020, 7:20pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/2 "2020-01-03T19:20:53Z")

</div>

I don’t know about what Juno does, but I assume it has something to do with why you are seeing everything on one thread here (though it does seem quite strange that you still get `nthreads() == 6`.

You don’t actually need multiple processes to run multiple threads. Using `--proc` creates separate Julia processes (separate in the sense that they each have their own memory and are managed by the OS), which is the main reason it takes longer to start. You can run a single process with multiple threads by setting the `JULIA_NUM_THREADS` variable.

I would start by verifying that the same code runs on multiple threads when you run Julia from the terminal having set `JULIA_NUM_THREADS=6`.

---

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [January 3, 2020, 7:40pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/3 "2020-01-03T19:40:25Z")

</div>

I tried also this, the number of threads is still 1.

---

<div class="post-metadata">

### Author: ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)
#### Post date: [January 3, 2020, 7:52pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/4 "2020-01-03T19:52:49Z")

</div>

I could not reproduce your results, either from Juno or a plain julia repl launched from the terminal. I did the following (julia 1.3.0 on Linux).

From a shell prompt I did:

```julia
~ export JULIA_NUM_THREADS=6

```

Then I launched atom from that same shell:

```julia
~ atom

```

Then from the julia repl inside juno I saw the following:

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.3.0 (2019-11-26)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> Threads.nthreads()
6

julia> Threads.@threads for i = 1:10
                   println("i = $i on thread $(Threads.threadid())")
                 end
i = 3 on thread 2
i = 4 on thread 2
i = 10 on thread 6
i = 5 on thread 3
i = 9 on thread 5
i = 6 on thread 3
i = 7 on thread 4
i = 8 on thread 4
i = 1 on thread 1
i = 2 on thread 1

```

I saw the same thing when launching a plain julia repl from that same shell session.

---

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [January 3, 2020, 7:57pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/5 "2020-01-03T19:57:22Z")

</div>

I restarted the juno shell and it works 😔

However I still cannot start julia from console with more than 1 threads.

I set `JULIA_NUM_THREADS=6` and as expected `echo $JULIA_NUM_THREADS` prints `6` but when I start julia i get:

```julia
julia> Threads.nthreads()
1

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [January 3, 2020, 8:05pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/6 "2020-01-03T20:05:06Z")

</div>

Did you do `export JULIA_NUM_THREADS=6` or just `JULIA_NUM_THREADS=6` ?

You need the `export`, otherwise the value of the environment variable is not passed into any new processes (in this case the Julia process). This isn’t a Julia-specific thing–it’s just the way your shell works.

```julia
$ MY_VAR=my_value
$ echo $MY_VAR
my_value
$ julia -E 'ENV["MY_VAR"]'
ERROR: KeyError: key "MY_VAR" not found
Stacktrace:
 [1] (::getfield(Base, Symbol("##457#458")))(::String) at ./env.jl:79
 [2] access_env at ./env.jl:43 [inlined]
 [3] getindex(::Base.EnvDict, ::String) at ./env.jl:79
 [4] top-level scope at none:1
$ export MY_VAR=my_value
$ echo $MY_VAR
my_value
$ julia -E 'ENV["MY_VAR"]'
"my_value"

```

---

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [January 3, 2020, 8:07pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/7 "2020-01-03T20:07:47Z")

</div>

Oh, I didn’t ☹  
That solves it. Thanks!

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [January 3, 2020, 8:22pm UTC](https://discourse.julialang.org/t/multi-threading-bug/32937/8 "2020-01-03T20:22:12Z")

</div>

Running `julia -p 4` starts four _processes_, not four threads.
