W3cubDocs

/Crystal 0.31

class Channel(T)

Overview

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

Defined in:

channel.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(capacity = 0)Source

Class Method Detail

def self.receive_first(channels : Tuple | Array)Source

def self.receive_first(*channels)Source

def self.select(ops : Indexable(SelectAction), has_else = false)Source

def self.select(*ops : SelectAction)Source

def self.send_first(value, channels : Tuple | Array)Source

def self.send_first(value, *channels)Source

Instance Method Detail

def closeSource

def closed?Source

def inspect(io : IO) : NilSource

Description copied from class Reference

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>

def pretty_print(pp)Source

def receiveSource

Receives a value from the channel. If there is a value waiting, it is returned immediately. Otherwise, this method blocks until a value is sent to the channel.

Raises ClosedError if the channel is closed or closes while waiting for receive.

channel = Channel(Int32).new
channel.send(1)
channel.receive # => 1

def receive?Source

Receives a value from the channel. If there is a value waiting, it is returned immediately. Otherwise, this method blocks until a value is sent to the channel.

Returns nil if the channel is closed or closes while waiting for receive.

def receive_impl(&block)Source

def receive_internal(&block)Source

def send(value : T)Source

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/Channel.html