# Threads.@spawn not using master thread

**URL:** <https://discourse.julialang.org/t/threads-spawn-not-using-master-thread/31442>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [November 24, 2019, 3:19am UTC](https://discourse.julialang.org/t/threads-spawn-not-using-master-thread/31442 "2019-11-24T03:19:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ignis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ignis/32/12938_2.png) [@ignis](https://discourse.julialang.org/u/ignis)\
**Post date:** [November 24, 2019, 3:19am UTC](https://discourse.julialang.org/t/threads-spawn-not-using-master-thread/31442/1 "2019-11-24T03:19:29Z")

</div>

I’ve been using Julia to parallelize some algorithms, so I’m also studying how julia parallel programming works.  
I did a simple code to check the behavior of Threads.@spawn and I’ve noticed it would only use the master thread the first time I called the function with the macro, if I kept calling it, it would not use the master thread anymore.  
Could someone explain this behavior to me?

This is the code I used is this one. The call\_threads is the array I count how many times the thread is invoked, I noticed it stores a value the first time I call the function, but the value that corresponds to the first thread doesn’t change in the following calls.

```julia
max_thread = zeros(Threads.nthreads())
call_threads = zeros(Threads.nthreads())

function generatingNumber()
    candidate = (rand() * 20)
    if (candidate > max_thread[Threads.threadid()])
        max_thread[Threads.threadid()] = candidate
    end
    call_threads[Threads.threadid()] +=1
end

function runthreads()
    global max_thread
    thread = Task[]
    for i in 1:100
        push!(thread, Threads.@spawn generatingNumber())
    end
    for i in 1:length(thread)
        wait(thread[i])
    end
    max_value = maximum(max_thread)
    max_thread = zeros(Threads.nthreads())
end

```

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [November 24, 2019, 4:38am UTC](https://discourse.julialang.org/t/threads-spawn-not-using-master-thread/31442/2 "2019-11-24T04:38:04Z")

</div>

Since your task workload is trivial, it takes longer to allocate and schedule the task than to execute it. So by the time the master thread exits the push loop, most or all of the work has been done by the others. `@spawn` takes hundreds of cycles, whereas `rand()` seems to average around 20.

---

<div class="post-metadata">

**Author:** ![ignis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ignis/32/12938_2.png) [@ignis](https://discourse.julialang.org/u/ignis)\
**Post date:** [November 24, 2019, 4:56am UTC](https://discourse.julialang.org/t/threads-spawn-not-using-master-thread/31442/3 "2019-11-24T04:56:37Z")

</div>

Indeed, I added a sleep in the `generatingNumber` function and it kept spawning at master thread. I still don’t understand why before that it would spawn at first thread only at first call.
