# Why does Theads.foreach only take Channels?

**URL:** <https://discourse.julialang.org/t/why-does-theads-foreach-only-take-channels/103592>\
**Category:** General Usage\
**Created:** [September 6, 2023, 10:08pm UTC](https://discourse.julialang.org/t/why-does-theads-foreach-only-take-channels/103592 "2023-09-06T22:08:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [September 6, 2023, 10:08pm UTC](https://discourse.julialang.org/t/why-does-theads-foreach-only-take-channels/103592/1 "2023-09-06T22:08:47Z")

</div>

Below you can find some confusion I had earlier. Is there a reason that `Threads.foreach` can’t take an array?

## Previous confusion

Can someone explain why this code isn’t printing each element of the array?

```julia
julia> function make_ch(arr)
           @show "in make"
           return Channel() do ch
               @show "in channel" arr
               Threads.foreach(arr) do e
                   @show "in foreach"
                   @show e
               end
               @show "exiting channel"
           end
       end
make_ch (generic function with 1 method)

julia> make_ch(collect(1:10))
"in make" = "in make"
"in channel" = "in channel"
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Channel{Any}(0) (closed)

julia> versioninfo()
Julia Version 1.10.0-beta2
Commit a468aa198d (2023-08-17 06:27 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, tigerlake)
  Threads: 11 on 8 virtual cores
Environment:
  JULIA_EDITOR = code

```

I thought I had my code working on 1.10.0-beta1 and now it is broken after upgrade. This MWE makes me think there might be a bug? But I might be totally missing something.

PS, it works when I switch to regular `foreach`

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [September 6, 2023, 10:18pm UTC](https://discourse.julialang.org/t/why-does-theads-foreach-only-take-channels/103592/2 "2023-09-06T22:18:55Z")

</div>

I’m a dork. `Threads.foreach` takes a `Channel`.
