# What is julia doing with your threads?

**URL:** <https://discourse.julialang.org/t/what-is-julia-doing-with-your-threads/110052>\
**Category:** General Usage\
**Created:** [February 11, 2024, 12:07am UTC](https://discourse.julialang.org/t/what-is-julia-doing-with-your-threads/110052 "2024-02-11T00:07:10Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 12, 2024, 10:30am UTC](https://discourse.julialang.org/t/what-is-julia-doing-with-your-threads/110052/21 "2024-02-12T10:30:34Z")

</div>

Somewhat related [discussion](https://github.com/JuliaLang/julia/issues/53217#issuecomment-1930888991) around that the main thread is in the `:default` or `:interactive` pool depending on if one starts Julia with interactive threads.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 21, 2024, 7:59pm UTC](https://discourse.julialang.org/t/what-is-julia-doing-with-your-threads/110052/22 "2024-02-21T19:59:49Z")

</div>

> [@PetrKryslUCSD](#):
>
> ```julia
> s = Float64[]
> Threads.@spawn let r = $ch[1], i = $ch[2]
> push!(s, work(r))
> end
> 
> ```

That’s not thread-safe. Breaking thread-safety not only affects accuracy, but it often ends up harming performance, too. I highly suspect that (or GC, as in the other thread that reminded me of this one) as being the problem here.

---

<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:** [February 21, 2024, 9:50pm UTC](https://discourse.julialang.org/t/what-is-julia-doing-with-your-threads/110052/23 "2024-02-21T21:50:42Z")

</div>

Somehow I don’t see how the compiler could mind the access to `s`.  
Otherwise it would know it was not thread-safe and it could be made  
be thread-safe by the compiler… 😉 But I don’t think it does.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 21, 2024, 10:04pm UTC](https://discourse.julialang.org/t/what-is-julia-doing-with-your-threads/110052/24 "2024-02-21T22:04:26Z")

</div>

The problem isn’t the access, it’s the mutation. The threads will trample over eachother as they all try to grow the memory.

This is definitely a problem:

```julia
julia> function f(s, n)
           Threads.@threads for i in 1:n
               push!(s, i)
           end
           return s
       end
f (generic function with 1 method)

julia> f(Float64[], 10000)
4630-element Vector{Float64}:
    1.0
    2.0
    3.0
    ⋮
 6249.0
 6250.0

julia> length(f(Float64[], 10000))
3162

julia> length(f(Float64[], 10000))
6250

julia> length(f(Float64[], 10000))
1678

julia> minimum(f(Float64[], 10000))
5.2150196471021e-310

```

[Previous page](https://discourse.julialang.org/t/what-is-julia-doing-with-your-threads/110052.md?page=1)
