Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Concurrency and Fibers

select, Mutex and Parallelism in Crystal

Learn Crystal select over several channels, timeouts, Mutex, Atomic counters, race conditions and when fibers run in parallel with preview_mt, with Ruby stand-ins.

A single channel is simple. Real programs wait on several things at once (a result, a timeout, a cancel signal) and sometimes share data between fibers that truly run in parallel. This lesson covers select, timeouts, Mutex and atomic values.

The Crystal listings are read-only. Ruby stand-ins use Thread::Queue, Timeout, Mutex and Monitor, which work the same way conceptually.

select: wait for the first channel that is ready

select takes several channel operations and runs the branch of the first one that can proceed:

CRYSTAL
fast = Channel(String).new
slow = Channel(String).new

spawn { sleep 5.milliseconds;  fast.send("fast") }
spawn { sleep 50.milliseconds; slow.send("slow") }

select
when msg = fast.receive
  puts "got #{msg}"      # this branch wins
when msg = slow.receive
  puts "got #{msg}"
end

Each when is either a receive or a send. If several are ready at once Crystal picks one at random, so no channel can starve the others. Add an else branch to make select non-blocking: it runs at once when no when is ready.

CRYSTAL
select
when value = jobs.receive
  process value
else
  puts "nothing to do right now"
end
Ruby Ruby stand-in: wait for the first of two queues

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.

Timeouts

Crystal offers timeout inside select, which creates a channel that fires after a duration:

CRYSTAL
result = Channel(Int32).new
spawn { sleep 100.milliseconds; result.send(42) }

select
when value = result.receive
  puts "answer: #{value}"
when timeout(10.milliseconds)
  puts "gave up after 10 ms"
end

The timeout branch wins here because the worker is slower. Use timeouts around every network call; a hung connection should never freeze a request forever. Newer releases also offer receive? with timeout: style helpers and Fiber::Timeout types, but the select/timeout pattern works everywhere.

Ruby Ruby stand-in: a timeout around slow 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.

Data races and Mutex

When fibers run on a single thread, nothing interrupts them between yield points, so simple counters are safe in most programs. As soon as you add worker threads (-Dpreview_mt) two fibers can update the same variable at the same instant, and increments get lost:

CRYSTAL
counter = 0
wg = WaitGroup.new(4)

4.times do
  spawn do
    10_000.times { counter += 1 }   # data race with preview_mt: result may be < 40000
    wg.done
  end
end
wg.wait

The fix is a Mutex, which lets only one fiber at a time into a protected section:

CRYSTAL
counter = 0
lock = Mutex.new

4.times do
  spawn do
    10_000.times do
      lock.synchronize { counter += 1 }
    end
  end
end

Keep the locked section short, never block (I/O, sleep, channel waits) inside it, and always use synchronize, which releases the lock even when an exception is raised. If you lock two mutexes, always lock them in the same order, or you invite a deadlock.

Ruby Ruby stand-in: Mutex protects a counter

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.

Atomic values

For a lone counter or flag, a lock is heavy. Atomic(T) (and in newer versions Atomic::Flag) provides lock-free operations:

CRYSTAL
hits = Atomic(Int32).new(0)

100.times do
  spawn { hits.add(1) }
end
Fiber.yield
puts hits.get

flag = Atomic::Flag.new
flag.test_and_set    # true the first time
flag.test_and_set    # false afterwards

Use atomics for single values; use a Mutex when several variables must change together, or a channel when the data can simply be handed over to one owner.

Sharing less: ownership by passing

The calmest concurrent programs share almost nothing. Instead of locking a shared hash, make one fiber own it and talk to it:

CRYSTAL
struct Command
  getter key : String
  getter reply : Channel(Int32)
  def initialize(@key, @reply); end
end

requests = Channel(Command).new
store = {"a" => 1, "b" => 2}

spawn do
  while cmd = requests.receive?
    cmd.reply.send(store[cmd.key]? || 0)
  end
end

reply = Channel(Int32).new
requests.send(Command.new("a", reply))
puts reply.receive   # 1

Only one fiber touches store, so no lock is needed. This "owner fiber" pattern is easy to reason about and is how the worker pool project keeps its statistics.

flowchart TD
  Q{"Is the data shared between parallel fibers?"} -->|no| A["Nothing needed"]
  Q -->|yes| B{"One simple number or flag?"}
  B -->|yes| C["Atomic"]
  B -->|no| D{"Can one fiber own it?"}
  D -->|yes| E["Channel to the owner"]
  D -->|no| F["Mutex#synchronize"]

Pick the lightest tool that keeps shared data safe.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does select do when two when branches are ready at once?

    A random choice prevents one busy channel from starving the others.
  2. 2. How do you give up waiting after 10 ms inside a select?

    timeout(duration) returns a channel that fires after the duration, so it can be a when branch.
  3. 3. Why use lock.synchronize { ... } instead of lock.lock and lock.unlock?

    synchronize wraps the block in an ensure, so an exception cannot leave the mutex locked.
  4. 4. Which approach needs no lock at all?

    If only one fiber touches the data, there is nothing to race on.

Summary

  • select waits on several channel operations and runs the first ready one; else makes it non-blocking.
  • timeout(duration) inside select limits waiting.
  • Data races appear when fibers run in parallel; a Mutex with synchronize protects shared state.
  • Atomic suits single counters and flags.
  • Prefer designs where one fiber owns the data.

Next: Shards and Dependencies