Title here
Summary here
Our example demonstrates how to use select
to wait on multiple channel operations. Combining fibers and channels with select
is a powerful feature of Crystal.
We receive the values "one"
and then "two"
as expected.
Note that the total execution time is only ~2 seconds since both the 1 and 2 second sleep
operations execute concurrently.
In this Crystal version:
Channel(String).new
to create channels instead of make(chan string)
.spawn
to create new fibers.select
syntax in Crystal is very similar to the original, but we use when
instead of case
.sleep
with a duration (e.g., 1.second
) instead of time.Sleep
.send
and receive
instead of the <-
operator.The overall structure and functionality of the program remain the same, demonstrating how to use select
to efficiently wait on multiple channel operations in Crystal.