class

Channel(T)

Inherits Iterator < Enumerable < Reference < Object

A Channel enables concurrent communication between fibers.

They allow communicating data between fibers without sharing memory and without having to worry about locks, semaphores or other special structures.

channel = Channel(Int32).new

spawn do
  channel.send(0)
  channel.send(1)
end

channel.receive # => 0
channel.receive # => 1

NOTE: Although a Channel(Nil) or any other nilable types like Channel(Int32?) are valid they are discouraged since from certain methods or constructs it receiving a nil as data will be indistinguishable from a closed channel.

Instance methods

try_receive?
Source
try_send(value : T) : Bool

Sends a value to the channel if it has capacity. It doesn't block if the channel doesn't have capacity.

Returns true if the value could be deliviered immediately, false otherwise. Raises ClosedError if the channel is closed or closes while waiting on a full channel.

Source
try_send?(value : T) : Bool

Sends a value to the channel if it has capacity. It doesn't block if the channel doesn't have capacity.

Returns true if the value could be deliviered immediately, false otherwise, also when the channel is closed.

Source