Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Concurrency and Fibers

Fibers and spawn in Crystal

Understand Crystal concurrency: lightweight fibers, spawn, the cooperative scheduler, sleep and Fiber.yield, why main exiting stops everything, with Ruby stand-ins.

Crystal's approach to concurrency is borrowed from Go: you start many tiny tasks, called fibers, and let them talk to each other through channels. A fiber is not an operating-system thread. It is a very light unit of work (a few kilobytes of stack) that the Crystal runtime schedules itself. You can run hundreds of thousands of them in one process.

The Crystal listings are read-only. Ruby has fibers too, but without Crystal's built-in scheduler, so our stand-ins use Fiber with a small hand-written scheduler, plus Ruby threads for the "many at once" demos. They are labelled where they differ.

Starting a fiber

CRYSTAL
spawn do
  puts "hello from a fiber"
end

puts "hello from main"
sleep 10.milliseconds   # give the fiber a chance to run

spawn takes a block and schedules it to run. It returns immediately; the block does not run right away. The new fiber starts the next time the current fiber gives up control. Here that happens at sleep. If main ended without waiting, the program would exit and the spawned fiber would never get to print: when the main fiber finishes, the program ends, no matter what other fibers are doing.

A fiber gives up control (a yield point) when it:

  • calls sleep,
  • waits on a channel (receive or send),
  • performs I/O that would block (reading a socket, gets, an HTTP call),
  • calls Fiber.yield.

Between yield points a fiber runs without interruption. This is cooperative scheduling: Crystal does not preempt a fiber in the middle of its work. A tight loop that never does I/O will hog the thread and starve the others.

CRYSTAL
3.times do |i|
  spawn do
    puts "fiber #{i} starts"
    sleep 1.millisecond
    puts "fiber #{i} ends"
  end
end

sleep 20.milliseconds

The output order is deterministic enough in practice (0, 1, 2, then 0, 1, 2) but you should never depend on it.

Ruby Ruby stand-in: a tiny cooperative scheduler

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.

Waiting for fibers

You rarely use sleep to wait in real code. Use a channel (see Channels) or the WaitGroup type, available in recent Crystal versions:

CRYSTAL
require "wait_group"

results = Array(Int32).new(3, 0)

WaitGroup.wait do |wg|
  3.times do |i|
    wg.spawn do
      sleep (3 - i).milliseconds
      results[i] = i * 10
    end
  end
end

puts results   # [0, 10, 20]

WaitGroup.wait returns only when every fiber started inside it has finished. With older compilers, the same job is done with a channel that each fiber sends a value to.

Capturing variables safely

A fiber's block is a closure (see Blocks, Procs and yield), so it sees the variables around it. Watch out for a loop variable changed after spawn:

CRYSTAL
i = 0
while i < 3
  spawn { puts i }     # all fibers may see the final value, 3
  i += 1
end
Fiber.yield

Pass the value into the fiber instead. spawn accepts a name for debugging, and a common trick is to copy the value into a local variable inside a block-based loop:

CRYSTAL
3.times do |i|          # a new `i` for each iteration
  spawn { puts i }
end
Ruby Ruby stand-in: the loop-variable trap

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.

Fibers and threads

By default, a Crystal program runs all fibers on one thread. That is concurrency, not parallelism: while one fiber waits for I/O, another runs, but two fibers never run at the very same instant. This is ideal for network servers that spend most of their time waiting, and it removes most data races, since nothing interrupts a fiber between yield points.

Parallelism is opt-in with the -Dpreview_mt compile flag (and, in newer releases, -Dexecution_context), which adds worker threads. Then fibers may run truly in parallel, and shared data needs care: use a Mutex or atomic types (see select, Mutex and Parallelism).

CRYSTAL
# crystal build -Dpreview_mt app.cr ; CRYSTAL_WORKERS=4 ./app
Ruby Ruby stand-in: real threads for waiting work

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.

Fiber lifecycle

stateDiagram-v2
  [*] --> Ready: spawn
  Ready --> Running: scheduler picks it
  Running --> Waiting: sleep, channel, I/O
  Waiting --> Ready: event arrives
  Running --> Ready: Fiber.yield
  Running --> [*]: block ends

A fiber moves between ready, running and waiting; only the running fiber uses the CPU.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens when the main fiber ends while other fibers are still running?

    Crystal exits when main finishes, so you must wait for your fibers with a channel, WaitGroup or sleep.
  2. 2. When does a spawned fiber actually start running?

    spawn only schedules the fiber; scheduling is cooperative.
  3. 3. By default, how many threads run your fibers?

    Without -Dpreview_mt all fibers share one thread, giving concurrency without parallelism.
  4. 4. Why can a while loop with spawn { puts i } print the wrong number?

    The closure captures the variable, not its value at spawn time; a block parameter creates a fresh variable per iteration.

Summary

  • spawn { ... } schedules a fiber; it does not run until the current fiber yields.
  • Fibers are cheap and cooperatively scheduled on one thread by default.
  • The program ends when main ends: wait with channels or WaitGroup.
  • Fibers close over variables, so beware of shared loop variables.
  • Parallelism is an opt-in compile flag.

Next: Channels: Talking Between Fibers