Course of Raku / Functional, concurrent, reactive, and web programming / Concurrent programming / Channels / Closing a channel
Quiz — Closing a channel
What does the following program print?
my $c = Channel.new;
$c.send(1);
$c.send(2);
$c.close;
say $c.list.elems;| 0 | 0 |
| 0 | 1 |
| 1 | 2 |
| 0 | it waits forever |
.list collects every value still in the channel and
finishes because the channel is closed. Two values were sent, so
.elems is 2. Without the .close,
.list would wait forever.