Playground · Concurrency · 33 of 33
channels
Channels: tasks talk by sending values, not by sharing them.
# Channels: tasks talk by sending values, not by sharing them. jobs = Channel(Int32).new(8) results = Channel(Int32).new(8) group do |g| # Two workers. Each takes jobs until the channel closes. 2.times do g.spawn do loop do case jobs.receive in Int32 then results.send(it * 10) in ChannelClosed then break in Cancelled then break end end 0 end end # One producer. Closing says there will be no more. g.spawn do (1..5).each { |n| jobs.send(n) } jobs.close 0 end # Collect five answers. total = 0 5.times do case results.receive in Int32 then total += it in ChannelClosed then break in Cancelled then break end end puts total end
150
exit 0, on Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+
The pane no longer holds samples/tour/channels.iyi. What the output pane shows is the recorded run of the recorded file, not of what you typed. How this works
recorded with iyi 0.18.0 (built on Crystal 1.22.0-dev [2a9653052] (2026-10-18)) · Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+ · 2026-10-19
This program does not compile for the browser's target, so this page shows what it printed when the compiler ran it natively, on the machine named above. You can still type in the pane, and the page will say when what is in it is no longer the file the recording is about.
Tasks and channels run on Linux, macOS arm64 and Windows x86-64, and not on wasm32 yet: a program that names `group` or `Channel` there does not compile, because src/iyi/concurrency.iyi refuses to ship a spelling without the feature (SPEC.md III.4.8). What you see is what the program printed when the compiler ran it natively. How this works
Try it on your machine
Copy the program into channels.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.
$ iyi run channels.iyi