Channel (programming)

This article is about software interprocess communication. It is not to be confused with Channel (communications).

In computing, a channel is a model for interprocess communication and synchronization via message passing. 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 to, as a stream. Different implementations of channels may be buffered or not, and either synchronous or asynchronous.

Channels are fundamental to the process calculus approach to concurrency, and originated in communicating sequential processes (CSP), a formal model for concurrency, and has been used in many derived languages, such as occam, and Limbo programming language (via Newsqueak and the Alef programming language). They are also used in the C programming language threading library libthread, and in Plan 9 from Bell Labs, which uses libthread, as well as in Stackless Python and the Go programming language.

Channel implementations

Channels modeled after the CSP model are inherently synchronous: a process waiting to receive an object from a channel will block until the object is sent. This is also called rendezvous behaviour. Typical supported operations are presented below using the example of the libthread channel API.

libthread channels

The Multithreading library, libthread, which was first created for the operating system Plan 9, offers inter-thread communication based on fixed-size channels.

OCaml events

The OCaml 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

XMOS XC

The XMOS programming language XC provides a primitive type "chan" and two operators "<:" and ":>" for sending and receiving data from a channel.[1]

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 {
  c <: 42;
  c :> x;
}

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.[2]

c := make(chan int)

go func() {c <- 42}()

x := <- c

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, where a future is a one-element channel, and a promise is a process that sends to the channel, fulfilling the future.[3] Similarly, iterators can be constructed directly from channels.[4]

References

External links

This article is issued from Wikipedia - version of the 12/5/2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.