Channel
A Go-style channel for communicating between concurrent processes.
Channels are the primary mechanism for passing values between forked
processes. An unbuffered channel (Channel new) blocks the sender
until a receiver is ready, and vice versa. A buffered channel
(Channel new: n) allows up to n values to be queued before the
sender blocks.
Channel new isClosed >>> false
(Channel new: 5) capacity >>> 5
(Channel new: 3) isEmpty >>> true
(Channel new: 3) size >>> 0
ch := Channel new: 5.
ch trySend: 42.
ch tryReceive >>> 42
ch := Channel new: 1.
ch trySend: 'hello' >>> true
ch := Channel new: 1.
ch close.
ch isClosed >>> true
"Producer-consumer with a buffered channel"
| ch <Channel> |
ch := Channel new: 10.
[1 to: 10 do: [:i | ch send: i]. ch close] fork.
[ch isClosed not] whileTrue: [ch receive]
Class Methods
primitives
Create a new unbuffered channel.
Create a buffered channel with the given capacity.
Wait on multiple channel operations. Returns the result of the first ready case.
Non-blocking select. If no case is ready, evaluate block.
Instance Methods
primitives
Return the buffer capacity of the channel.
Return true if no values are buffered.
Create a select case that receives from this channel and evaluates block with the value.
Return the number of buffered values.
uncategorized
Close the channel. After closing, sends will fail and receives
will drain any remaining buffered values, then return nil.
ch := Channel new: 1.
ch close.
ch isClosed >>> true
Return true if the channel has been closed.
Channel new isClosed >>> false
Stream-compatible alias for receive. Receives a value from
the channel.
| ch <Channel> |
ch := Channel new: 1.
ch nextPut: 42.
ch next
Stream-compatible alias for send:. Sends a value on the channel.
| ch <Channel> |
ch := Channel new: 1.
ch nextPut: 'hello'.
ch next
Return a string representation of the channel.
Channel new printString >>> 'a Channel'
Receive a value from the channel. Blocks until a value is available or the channel is closed.
| ch <Channel> |
ch := Channel new: 1.
ch send: 42.
ch receive
Send a value on the channel. Blocks until a receiver is ready (unbuffered) or until buffer space is available (buffered).
| ch <Channel> |
ch := Channel new: 1.
ch send: 'hello'.
ch receive
Attempt to receive a value without blocking. Returns the value
if one was available, or nil if the channel was empty.
ch := Channel new: 1.
ch trySend: 99.
ch tryReceive >>> 99
(Channel new: 1) tryReceive >>> nil
Attempt to send a value without blocking. Returns true if
the value was sent, false if the channel was full or closed.
ch := Channel new: 2.
ch trySend: 'a' >>> true
ch := Channel new: 1.
ch trySend: 'a'.
ch trySend: 'b' >>> false