# @threads is really working in parallel?

**URL:** https://discourse.julialang.org/t/threads-is-really-working-in-parallel/25788
**Category:** Julia at Scale
**Tags:** multithreading
**Created:** [June 28, 2019, 7:24am UTC](https://discourse.julialang.org/t/threads-is-really-working-in-parallel/25788 "2019-06-28T07:24:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kwild](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@kwild](https://discourse.julialang.org/u/kwild)
#### Post date: [June 28, 2019, 7:24am UTC](https://discourse.julialang.org/t/threads-is-really-working-in-parallel/25788/1 "2019-06-28T07:24:45Z")

</div>

Hi,

on my system:

```julia
julia> versioninfo()
Julia Version 1.1.1
Commit 55e36cc308 (2019-05-16 04:10 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, haswell)
Environment:
  JULIA_EDITOR = "C:\Users\WildKev\AppData\Local\atom\app-1.38.2\atom.exe" -a
  JULIA_NUM_THREADS = 4

```

the following code

```julia
using Plots

a = falses(1000)
a[100] = true

function tfunc(a)
   stop = Threads.Atomic{Bool}(false)
   threadid = zeros(size(a))

   Threads.@threads for i = 1:length(a)
     threadid[i] = Threads.threadid()
     stop[] && break
     a[i] && Threads.atomic_xchg!(stop, true)
   end

   return threadid
end

plot(tfunc(a))

```

produces the following threadid over element plot.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/0/00e6e46014ca1acdb4d86ca1590f5ed606ffab0f.png)

It looks like the splitted iteration space runs sequential.

---

<div class="post-metadata">

### Author: ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)
#### Post date: [June 28, 2019, 7:35am UTC](https://discourse.julialang.org/t/threads-is-really-working-in-parallel/25788/2 "2019-06-28T07:35:52Z")

</div>

what does `Threads.nthreads()` say?

---

<div class="post-metadata">

### Author: ![kwild](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@kwild](https://discourse.julialang.org/u/kwild)
#### Post date: [June 28, 2019, 10:44am UTC](https://discourse.julialang.org/t/threads-is-really-working-in-parallel/25788/3 "2019-06-28T10:44:02Z")

</div>

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

```

In the plot the y-axis also indicates that they are in use.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 28, 2019, 11:31am UTC](https://discourse.julialang.org/t/threads-is-really-working-in-parallel/25788/4 "2019-06-28T11:31:08Z")

</div>

Which part makes you think “It looks like the splitted iteration space runs sequential.” Did you expect everything to start exactly at the same time?

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [June 28, 2019, 12:06pm UTC](https://discourse.julialang.org/t/threads-is-really-working-in-parallel/25788/5 "2019-06-28T12:06:59Z")

</div>

You are hitting thread start up time…it takes time to start the 3 additional threads.

If you change it to:

```julia
   a[600] = true

```

You will get some interesting graphs and on my machine (4 core) the result is different every run.

This might be more what you are looking for. Basically I give each thread 3 seconds to start and become stable before processing the array:

```julia
using Plots
using Dates

a = falses(1000)
a[100] = true

function tfunc(a)
   stop = Threads.Atomic{Bool}(false)
   threadid = zeros(size(a))

   start = (second(now()) + 3) % 60
   Threads.@threads for i = 1:length(a)
     while second(now()) != start
     end

     threadid[i] = Threads.threadid()
     stop[] && break
     a[i] && Threads.atomic_xchg!(stop, true)
   end

   return threadid
end

plot(tfunc(a))

```
