In computing, a channel is a model for
interprocess communication
In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. Typically, applications can use IPC, categoriz ...
and
synchronization via
message passing
In computer science, message passing is a technique for invoking behavior (i.e., running a program) on a computer. The invoking program sends a message to a process (which may be an actor or object) and relies on that process and its supporti ...
. A message may be sent over a channel, and another process or thread is able to receive messages sent over a channel it has a
reference
Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a '' name'' ...
to, as a
stream. Different implementations of channels may be buffered or not, and either synchronous or asynchronous.
libthread channels
The
multithreading library,
libthread, which was first created for the operating system
Plan 9 Plan 9 or Plan Nine may refer to:
Music
* Plan 9 (band), a psychedelic rock band from Rhode Island
* ''Plan 9'', an album by Big Guitars From Memphis with Rick Lindy
* "Plan 9", a song on the 1993 album ''Gorgeous'' by electronica band 808 Stat ...
, offers inter-thread communication based on fixed-size channels.
OCaml events
The
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, D ...
event module offers typed channels for synchronization. When the module's send and receive functions are called, they create corresponding send and receive events which can be synchronized.
Examples
Lua Love2D
The
Love2D library which is part of the
Lua
Lua or LUA may refer to:
Science and technology
* Lua (programming language)
* Latvia University of Agriculture
* Last universal ancestor, in evolution
Ethnicity and language
* Lua people, of Laos
* Lawa people, of Thailand sometimes referred t ...
programming language implements channels with push and pop operations similar to stacks. The pop operation will block so as long as there is data resident on the stack. A demand operation is equivalent to pop, except it will block until there is data on the stack
-- A string containing code which will be interpreted by a function such as loadstring(),
-- but on the C side to start a native thread.
local threadCode =
love.thread.getChannel("test"):push("Hello world!")
function love.load()
-- Start the thread.
thread = love.thread.newThread(threadCode)
thread:start()
-- The thread will block until "Hello world!" is popped off channel test's stack.
-- Because the channel can be popped from before the thread first executes, there may not be data on the stack.
-- in that case use :demand() instead of :pop() because :demand() will block until there is data on the stack and then return the data.
print(love.thread.getChannel("test"):demand())
-- The thread can now finish.
end
XMOS XC
The
XMOS programming language
XC provides a primitive type "chan" and two operators "<:" and ":>" for sending and receiving data from a channel.
In this example, two hardware threads are started on the XMOS, running the two lines in the "par" block. The first line transmits the number 42 through the channel while the second waits until it is received and sets the value of x. The XC language also allows asynchronous receiving on channels through a select statement.
chan c;
int x;
par
Go
This snippet of Go code performs similarly to the XC code. First the channel c is created, then a goroutine is spawned which sends 42 through the channel. When the number is put in the channel x is set to 42. Go allows channels to buffer contents, as well as non blocking receiving through the use of a select block.
c := make(chan int)
go func() ()
x := <- c
Rust
Rust
Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH), ...
provides asynchronous channels for communication between threads. Channels allow a unidirectional flow of information between two end-points: the
Sender
and the
Receiver
.
use std::sync::mpsc;
use std::thread;
fn main()
Applications
In addition to their fundamental use for interprocess communication, channels can be used as a primitive to implement various other concurrent programming constructs which can be realized as streams. For example, channels can be used to construct
futures and promises
In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown ...
, where a future is a one-element channel, and a promise is a process that sends to the channel, fulfilling the future. Similarly,
iterators can be constructed directly from channels.
List of implementations
List of non-standard, library based implementations of channels
* For C++:
**
stlab This implementation supports splits, and different merge and zip operations. Different executors can be attached to the individual nodes.
References
{{refend
External links
Libthread Channel ImplementationBell Labs and CSP ThreadsStackless.com – Channels
Inter-process communication
Go (programming language) software