Channel¶
Usage
use Channel;
or
import Channel;
This module contains the implementation of channels which can be used to move typed data between Chapel tasks.
A channel is a parallel-safe data structure that provides a mechanism for
concurrently executing functions to communicate by sending and receiving values
of a specified element type. A channel can be buffered or unbuffered. A
buffered channel has a maximum capacity specified by bufferSize
. There are
mainly three operations that can be performed on a channel.
channel.send
: Send a value to the channel.
channel.recv
: Receive a value from the channel.
channel.close
: Close a channel such that no other values/data can be sent to it.
The channel operations are blocking, i.e., the calling task will be suspended if an operation cannot be completed. The channel follows First-In-First-Out mechanism, i.e., the first value sent to the channel will be received first.
- record channel¶
- type eltType¶
- var channelObj: shared chan(eltType)¶
- proc init(type eltType, size = 0)¶
Initialize a channel
- Arguments
elt : type – The element type used for sending and receiving
size : int – Specify the maximum capacity for the channel
bufferSize
.
- proc lock()¶
- proc unlock()¶
- proc recv(out val: eltType): bool¶
Receive the first value in the channel buffer. It will suspend the calling task, until data is sent to the channel. If the channel is closed and the buffer is empty, it will return false indicating that the receive operation was not successful.
- Arguments
val : eltType – Storage for the received value.
- Returns
true if the receive was successful, else false.
- Return type
bool
- proc send(in val: eltType) throws¶
Send a value to the channel buffer. If the buffer is at maximum capacity it will suspend the waiting task, until there is space in the buffer or a receiving task awakes it. If a channel is closed no more data can be sent to it.
- Arguments
val : eltType – Data to be sent to the channel
- Throws
ChannelError – If
send
is called on a closed channel.
- proc close() throws¶
This function is used to close a channel indicating that no more data can be sent to it.
- Throws
ChannelError – If called on a closed channel.
- iter these()¶
Iterator to receive data from the channel until it is closed.