Last active
January 4, 2021 22:38
-
-
Save bicycle1885/055d51f68835ccedb26e5e1f1c3cb6e6 to your computer and use it in GitHub Desktop.
Multithreaded channel demonstration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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