Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 · Concurrency and Fibers

Channels in Crystal – Talking Between Fibers

Learn Crystal channels: Channel(T), send and receive, buffered channels, close and Channel::ClosedError, producer-consumer pipelines and fan-in, with Ruby stand-ins.

The Crystal motto for concurrency is borrowed from Go: do not communicate by sharing memory; share memory by communicating. Instead of letting fibers change the same variable, fibers pass values through a channel. A Channel(T) is a typed pipe: one side sends values of type T, the other receives them.

Crystal code is read-only on this site. The Ruby stand-ins use Thread::Queue and Thread::SizedQueue, which are the closest standard-library equivalents (Ruby also has Ractor::Port, but queues are simpler).

Send and receive

CRYSTAL
channel = Channel(String).new

spawn do
  channel.send("hello")
  channel.send("world")
end

puts channel.receive   # hello
puts channel.receive   # world

By default a channel is unbuffered: send waits until another fiber calls receive, and receive waits until someone sends. The two fibers meet at the channel, which is why unbuffered channels are also called rendezvous points. Because waiting is a yield point, other fibers run while one waits.

A channel's element type is part of its type, so Channel(String) refuses channel.send(1) at compile time. A channel of a union such as Channel(Int32 | String) is allowed.

Ruby Ruby stand-in: a producer and a consumer

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Buffered channels

Give a size and sends do not block until the buffer is full:

CRYSTAL
jobs = Channel(Int32).new(3)    # room for 3 values

jobs.send(1)
jobs.send(2)
jobs.send(3)           # still no waiting
# jobs.send(4)         # would wait here until somebody receives

puts jobs.receive      # 1  (first in, first out)

A buffer smooths out short bursts between a fast producer and a slow consumer. A large buffer hides back-pressure: if the producer is always faster, memory keeps growing. A small buffer makes the producer slow down, which is often what you want.

Ruby Ruby stand-in: a bounded buffer

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Closing a channel

Closing says "no more values will come". Receivers still get everything that was already sent, then they see the channel is closed:

CRYSTAL
numbers = Channel(Int32).new

spawn do
  3.times { |i| numbers.send(i) }
  numbers.close
end

while n = numbers.receive?     # receive? returns nil when closed and empty
  puts n
end
# 0 1 2

receive raises Channel::ClosedError after the channel is closed and drained; receive? returns nil instead, which fits a while loop neatly. Sending on a closed channel raises Channel::ClosedError too. Only the sender side should close a channel, because only it knows when the work is done.

A pipeline

Channels compose into stages. Each stage reads from one channel and writes to the next:

CRYSTAL
def generate(n : Int32) : Channel(Int32)
  out = Channel(Int32).new
  spawn do
    n.times { |i| out.send(i + 1) }
    out.close
  end
  out
end

def square(input : Channel(Int32)) : Channel(Int32)
  out = Channel(Int32).new
  spawn do
    while v = input.receive?
      out.send(v * v)
    end
    out.close
  end
  out
end

squares = square(generate(5))
while v = squares.receive?
  puts v
end
# 1 4 9 16 25

Closing propagates: when generate closes its channel, square falls out of its loop and closes its own. The main loop then ends without any counter or sleep.

Ruby Ruby stand-in: a two-stage pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Fan-out and fan-in

Several fibers can receive from the same channel (each value goes to exactly one of them), which spreads work. Several fibers can send to one results channel, which collects it:

CRYSTAL
jobs = Channel(Int32).new
results = Channel(String).new

3.times do |id|
  spawn do
    while job = jobs.receive?
      results.send("worker #{id} did job #{job}")
    end
  end
end

spawn do
  6.times { |j| jobs.send(j) }
  jobs.close
end

6.times { puts results.receive }

Which worker gets which job varies from run to run, so tests should count results rather than compare order. The worker pool project builds this out fully.

flowchart LR
  P["Producer"] -->|send| J(("jobs channel"))
  J -->|receive| W1["Worker 1"]
  J -->|receive| W2["Worker 2"]
  J -->|receive| W3["Worker 3"]
  W1 --> R(("results channel"))
  W2 --> R
  W3 --> R
  R --> M["main"]

Fan-out spreads jobs over workers; fan-in gathers their results.

Mistakes to avoid

  • Deadlock. If every fiber waits and nobody can send, Crystal prints Deadlock! All fibers are waiting (in a single-thread program the main fiber blocked forever on receive). Always make sure somebody sends or closes.
  • Forgetting to close. A while v = ch.receive? loop never ends if the sender never closes.
  • Closing twice or from the receiver side. Close once, from the sender.
  • Using a channel as a lock. For protecting shared state use a Mutex (see select, Mutex and Parallelism).

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does send do on an unbuffered Channel(Int32).new?

    An unbuffered channel is a rendezvous: sender and receiver must meet.
  2. 2. What is the difference between receive and receive? on a closed, empty channel?

    receive? is made for while value = ch.receive? loops.
  3. 3. Who should close a channel?

    Only the producer knows when it is done; closing from a receiver can make senders raise.
  4. 4. What is a good reason to use a small buffer?

    A full buffer blocks the sender, which prevents unbounded memory growth.

Summary

  • Channel(T) is a typed pipe between fibers; unbuffered channels make sender and receiver meet.
  • Channel(T).new(n) adds a buffer of n values.
  • close ends the stream; receive? returns nil after that.
  • Stages connected by channels form pipelines; many readers on one channel form a worker pool.
  • Avoid deadlocks by always sending or closing.

Next: select, Mutex and Parallelism