# Elegant threaded \`foreach\`

**URL:** https://discourse.julialang.org/t/elegant-threaded-foreach/67489
**Category:** New to Julia
**Tags:** parallel, multithreading
**Created:** [September 1, 2021, 10:20am UTC](https://discourse.julialang.org/t/elegant-threaded-foreach/67489 "2021-09-01T10:20:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![stepanzh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stepanzh/32/34851_2.png) [@stepanzh](https://discourse.julialang.org/u/stepanzh)
#### Post date: [September 1, 2021, 10:20am UTC](https://discourse.julialang.org/t/elegant-threaded-foreach/67489/1 "2021-09-01T10:20:07Z")

</div>

I have two methods of a function `thermo!`

```julia
function thermo!(cell::FEMCell, mix::Mixture)
    # do something with `cell`
    return nothing # note, I don't need to collect return value
end

thermo!(problem::Problem) =
    foreach(cell -> thermo!(cell, problem.mixture), getcells(problem))

```

The second method _item-independently_ applies the first to a `Vector{FEMCell}`. I want a parallel version of `thermo!(problem::Problem)` using threads. (As my laptop has 2 physical cores.)

My synchronization pattern is

1. Run using threads `thermo!(problem::Problem)`;
2. Wait until parallel `thermo!(problem::Problem)` ends;
3. Go further…

I found `Threads.@threads` macro and use it as a solution

```julia
function thermo!(problem::ExplicitDarcy)
    Threads.@threads for cell in getcells(problem)
        thermo!(cell, problem.mixture)
    end
    return nothing
end

```

But,

_ **Is there a more elegant solution?** _ (As I have more similar situations in my program.)

Something like

```julia
thermo!(problem::Problem) =
    Threads.@threaded_foreach foreach(cell -> thermo!(cell, problem.mixture), getcells(problem))

```

* * *

> **UPD**  
> I’m accidently missed `Base.Threads.foreach`. If it’s the solution, how to use `Channel` in my situation?

---

<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: [September 1, 2021, 11:41am UTC](https://discourse.julialang.org/t/elegant-threaded-foreach/67489/2 "2021-09-01T11:41:06Z")

</div>

> [@stepanzh](#):
>
> Is there a more elegant solution?

```julia
using ThreadsX

ThreadsX.foreach

```

---

<div class="post-metadata">

### Author: ![stepanzh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stepanzh/32/34851_2.png) [@stepanzh](https://discourse.julialang.org/u/stepanzh)
#### Post date: [September 1, 2021, 2:35pm UTC](https://discourse.julialang.org/t/elegant-threaded-foreach/67489/3 "2021-09-01T14:35:45Z")

</div>

Thank you, @jling. The package `ThreadsX` seems deeper than `Base.Threads`, but in my project I want to avoid third-party tools.

* * *

As a solution, I defined

```julia
tforeach(f, c) = (Threads.@threads for x in c; f(x); end; return nothing)

```

Which I found handy as the only I need is to replace `foreach` with `tforeach` in my source file.
