# @threads for only using master thread?

**URL:** https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725
**Category:** General Usage
**Tags:** multithreading
**Created:** [August 19, 2019, 5:06pm UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725 "2019-08-19T17:06:03Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dstarerstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dstarerstor/32/8958_2.png) [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)
#### Post date: [August 19, 2019, 5:06pm UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/1 "2019-08-19T17:06:04Z")

</div>

I just switched to 1.3 to try out implementing multithreaded algorithms. I tried a very simple loop, from the announcement post:

```julia
Threads.@threads for i = 1:10
   println(Threads.threadid(), " of ", Threads.nthreads())
end

```

However, this only printed `1 of 4` every time I ran it, no matter how many iterations. Clearly, `nthreads()` shows that I have 4 threads available - how do I make sure my loops use all of them?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 19, 2019, 5:10pm UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/2 "2019-08-19T17:10:33Z")

</div>

Did you set JULIA\_NUM\_THREADS?

---

<div class="post-metadata">

### Author: ![dstarerstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dstarerstor/32/8958_2.png) [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)
#### Post date: [August 19, 2019, 5:11pm UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/3 "2019-08-19T17:11:44Z")

</div>

I’m using juno, which I thought automatically set it to the number of cores. Since `nthreads()` returns `4`, doesn’t that mean that it has been properly set?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 19, 2019, 5:13pm UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/4 "2019-08-19T17:13:40Z")

</div>

Yes, that’s should do it. It’s possible that the number of iterations is too low and there’s only one batch of work.

---

<div class="post-metadata">

### Author: ![dstarerstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dstarerstor/32/8958_2.png) [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)
#### Post date: [August 19, 2019, 5:15pm UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/5 "2019-08-19T17:15:52Z")

</div>

I’ll try to make a bigger case to see if it works.

In the meantime, its worth noting that the multithreading in general definitely works:

```julia
Threads.@spawn Threads.threadid()

fetch(ans)

```

gives me the expected various ids

EDIT: All of a sudden I tried this exact same code again and it worked fine. I have no idea why it wasn’t working to begin with.

---

<div class="post-metadata">

### Author: ![Evgeniia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgeniia/32/11456_2.png) [@Evgeniia](https://discourse.julialang.org/u/Evgeniia)
#### Post date: [November 22, 2019, 9:10am UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/6 "2019-11-22T09:10:10Z")

</div>

Hello,  
I am having the same problem… I am trying to use 12 threads to perform a task. I checked that 12 threads are available to Julia by running:  
`Threads.nthreads()`  
which gives me 12.

Then I test if multithreading works on a simple example:

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

```

But this results in an array of "1"s, so Julia always uses just one thread. According to [https://docs.julialang.org/en/v1.0/manual/parallel-computing/#Multi-Threading-(Experimental)-1](https://docs.julialang.org/en/v1.0/manual/parallel-computing/#Multi-Threading-(Experimental)-1) Julia should distribute the task between the available threads…

I also tried to make z = zeros(100) but that doesn’t help.

Does somebody have an idea what could be wrong? It seems as if Julia still needs an extra “allowance” to use all the available threads.

Thank you!

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [November 23, 2019, 7:07pm UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/7 "2019-11-23T19:07:26Z")

</div>

Has you checked the JULIA\_NUM\_THREADS variable? I have just update that variable, and later I have run your code, obtaining different values in z array, one different for each thread.

---

<div class="post-metadata">

### Author: ![Evgeniia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgeniia/32/11456_2.png) [@Evgeniia](https://discourse.julialang.org/u/Evgeniia)
#### Post date: [November 24, 2019, 6:43am UTC](https://discourse.julialang.org/t/threads-for-only-using-master-thread/27725/8 "2019-11-24T06:43:23Z")

</div>

Yes, JULIA\_NUM\_THREADS variable was 12. I now solved the problem after restarting Julia 2 times from when it wasn’t working. Not sure what went wrong the first 2 times… Thank you for the reply!
