# Looking for code to solve (surely common) 'embarrassing parallelism' multithreading use-case

**URL:** <https://discourse.julialang.org/t/looking-for-code-to-solve-surely-common-embarrassing-parallelism-multithreading-use-case/53527>\
**Category:** Performance\
**Tags:** parallel, multithreading\
**Created:** [January 18, 2021, 9:37am UTC](https://discourse.julialang.org/t/looking-for-code-to-solve-surely-common-embarrassing-parallelism-multithreading-use-case/53527 "2021-01-18T09:37:52Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [January 18, 2021, 3:18pm UTC](https://discourse.julialang.org/t/looking-for-code-to-solve-surely-common-embarrassing-parallelism-multithreading-use-case/53527/10 "2021-01-18T15:18:24Z")

</div>

> [@oxinabox](#):
>
> The docs for `schedule` says it adds to the scheduler’s queue.

yes, I see you are right!

```julia
using .Threads, BenchmarkTools

# a silly function taking some time, returning its thread
f(n) = (sum((i for i in 1:n) .^ 2); threadid())

function show_load(threads)
    res = fill("", nthreads())
    foreach(i->res[i]*="*", threads)
    res
end

```

then

```julia
julia> @btime f(2_000)
  1.174 μs (2 allocations: 31.50 KiB)
1

julia> fetch.(map((_->Threads.@spawn f(2000)), 1:nthreads()))
8-element Array{Int64,1}:
 3
 2
 4
 5
 6
 7
 8
 1

```

If we put the same load on all tasks, all threads are employed. Even with unbalanced load (if M \>\> N | M: number of tasks, N: nthreads) the balance is quite good:

```julia
julia> show_load(fetch.(map(_->(Threads.@spawn f(rand(1:2000))), 1:500)))
8-element Array{String,1}:
 " ****************************************************"
 " ****************************************************************************************"
 " **************************************************************************"
 " *********************************"
 " ********************************************************"
 " ***********************************************************"
 " ***********************************************************************"
 " *******************************************************************"

```

I first had an other impression because in my applications/tasks usually I read first from a channel. In that case the load is very imbalanced:

```julia
g(n) = (yield(); f(n))

julia> show_load(fetch.(map(_->(Threads.@spawn g(rand(1:2000))), 1:500)))
8-element Array{String,1}:
 "*"
 " *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************"
 "*"
 "*"
 "*"
 "*"
 "*"
 "*"

```

---

_[View the full topic](https://discourse.julialang.org/t/looking-for-code-to-solve-surely-common-embarrassing-parallelism-multithreading-use-case/53527)._
