Skip to content

Instantly share code, notes, and snippets.

@bicycle1885
Last active January 4, 2021 22:38
Show Gist options
  • Save bicycle1885/055d51f68835ccedb26e5e1f1c3cb6e6 to your computer and use it in GitHub Desktop.
Save bicycle1885/055d51f68835ccedb26e5e1f1c3cb6e6 to your computer and use it in GitHub Desktop.
Multithreaded channel demonstration
using Base.Threads: @spawn, nthreads, threadid
N = 100
chan = Channel(nthreads()) do chan
for val in 1:N
put!(chan, val)
println("T$(threadid()) => put $(val)")
end
end
workers = map(1:nthreads()) do _
@spawn begin
for val in chan
println("T$(threadid()) => took $(val)")
end
println("T$(threadid()) => finished")
end
end
foreach(wait, workers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment