Top Level Namespace

Defined in:

Method Summary

Method Detail

def spawn(*, name : String | Nil = nil, same_thread = false, &block) : Fiber #

Spawns a new fiber.

The newly created fiber doesn't run as soon as spawned.

Example:

# Write "1" every 1 second and "2" every 2 seconds for 6 seconds.

ch = Channel(Nil).new

spawn do
  6.times do
    sleep 1
    puts 1
  end
  ch.send(nil)
end

spawn do
  3.times do
    sleep 2
    puts 2
  end
  ch.send(nil)
end

2.times { ch.receive }

[View source]