# Multithreading simultaneously in two functions

**URL:** <https://discourse.julialang.org/t/multithreading-simultaneously-in-two-functions/65579>\
**Category:** Performance\
**Tags:** multithreading\
**Created:** [July 31, 2021, 2:19am UTC](https://discourse.julialang.org/t/multithreading-simultaneously-in-two-functions/65579 "2021-07-31T02:19:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vkv](https://avatars.discourse-cdn.com/v4/letter/v/d9b06d/32.png) [@vkv](https://discourse.julialang.org/u/vkv)\
**Post date:** [July 31, 2021, 2:19am UTC](https://discourse.julialang.org/t/multithreading-simultaneously-in-two-functions/65579/1 "2021-07-31T02:19:26Z")

</div>

Is there a way to do use all 9 threads in the following functions?

```julia
function f1()
    Threads.@threads for i in 1:3
        println("MCMC")
        println("$(Threads.threadid())")
        sleep(3.0)
    end
    println("#############")
end

function f2()
    Threads.@threads for i in 1:3
        println("Model")
        f1()
        println("$(Threads.threadid())")
    end
end

```

Right now running this prints the following:

```julia
$ julia -t 9

> f2()

Model
Model
Model
MCMC
2
MCMC
1
MCMC
3
MCMC
3
MCMC
1
MCMC
2
MCMC
MCMC
MCMC
2
3
1
#############
#############
2
1
#############
3

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 31, 2021, 11:40am UTC](https://discourse.julialang.org/t/multithreading-simultaneously-in-two-functions/65579/2 "2021-07-31T11:40:26Z")

</div>

```julia
julia> function f1()
           @sync for i in 1:3
               Threads.@spawn begin
                   println("MCMC")
                   println("$(Threads.threadid())")
                   sleep(3.0)
                   end
               end
           println("#############")
       end

julia> function f2()
          @sync for i in 1:3
               println("Model")
               Threads.@spawn f1()
               println("$(Threads.threadid())")
           end
       end

julia> f2()
Model
1
Model
1
Model
1
MCMC
MCMC
MCMC
5
MCMC
MCMC
MCMC
3
2
MCMC
4
8
7
5
MCMC
7
MCMC
7
#############
#############
#############

```

something like this

---

<div class="post-metadata">

**Author:** ![vkv](https://avatars.discourse-cdn.com/v4/letter/v/d9b06d/32.png) [@vkv](https://discourse.julialang.org/u/vkv)\
**Post date:** [July 31, 2021, 12:30pm UTC](https://discourse.julialang.org/t/multithreading-simultaneously-in-two-functions/65579/3 "2021-07-31T12:30:11Z")

</div>

Thank you for the solution.

Can you briefly explain what’s happening in your code?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 31, 2021, 12:30pm UTC](https://discourse.julialang.org/t/multithreading-simultaneously-in-two-functions/65579/4 "2021-07-31T12:30:46Z")

</div>

> **[Announcing composable multi-threaded parallelism in Julia](https://julialang.org/blog/2019/07/multithreading/)**
>
> Announcing composable multi-threaded parallelism in Julia | Software performance depends more and more on exploiting multiple processor cores....
