Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Channel (programming)

From Wikipedia, the free encyclopedia
Model for interprocess communication and synchronization via message passing
This article is about software interprocess communication; it is not to be confused withChannel (communications) orI/O channel.

In computing, achannel is a model forinterprocess communication andsynchronization viamessage 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 areference to, as astream. Different implementations of channels may be buffered or not, and either synchronous or asynchronous.

libthread channels

[edit]

Themultithreading library,libthread, which was first created for the operating systemPlan 9, offers inter-thread communication based on fixed-size channels.

OCaml events

[edit]

TheOCaml 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

[edit]

Lua Love2D

[edit]

TheLove2D library which uses theLua 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.localthreadCode=[[    love.thread.getChannel("test"):push("Hello world!")]]functionlove.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

[edit]

TheXMOS programming languageXC 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.

chanc;intx;par{c<:42;c:>x;}

Go

[edit]

This snippet ofGo code performs similarly to the XC code. First the channel c is created, then agoroutine 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(chanint)gofunc(){c<-42}()x:=<-c

Rust

[edit]

Rust provides asynchronous channels for communication between threads. Channels allow a unidirectional flow of information between two endpoints: theSender and theReceiver.[3]

usestd::sync::mpsc;usestd::thread;fnmain(){let(tx,rx)=mpsc::channel();thread::spawn(move||{tx.send(123).unwrap();});letresult=rx.recv();println!("{:?}",result);}

Applications

[edit]

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

List of implementations

[edit]

List of non-standard, library-based implementations of channels

  • For Scala:
    • CSO -- Communicating Scala Objects[6] is a complete DSL for channel-based communication and concurrency whose semantic primitives are generalizations of the OCCAM primitives. CSO has been used since 2007 in the teaching of concurrent programming, and relevant lectures can be found with the ThreadCSO implementation.[7]
  • For C++:
    • stlab[8] This implementation supports splits, and different merge and zip operations. Different executors can be attached to the individual nodes.
  • For Rust:
    • Tokio
    • async-channel
    • crossbeam

References

[edit]
  1. ^"XMOS Programming Guide | XMOS". Archived fromthe original on 2016-03-04. Retrieved2015-05-10.
  2. ^"Effective Go - the Go Programming Language".
  3. ^"Channels - Rust By Example".doc.rust-lang.org. Retrieved28 November 2020.
  4. ^"FuturesArchived 2020-12-04 at theWayback Machine",Go Language PatternsArchived 2020-11-11 at theWayback Machine
  5. ^"IteratorsArchived 2020-10-15 at theWayback Machine",Go Language PatternsArchived 2020-11-11 at theWayback Machine
  6. ^Sufrin, Bernard (2021-07-13),ThreadCSO(PDF), retrieved2023-02-17
  7. ^Sufrin, Bernard (2021-07-13),ThreadCSO, retrieved2023-02-17
  8. ^"stlab is the ongoing work of what was Adobe's Software Technology Lab. The Adobe Source Libraries (ASL), Platform Libraries, and new stlab libraries are hosted on github". 2021-01-31.

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Channel_(programming)&oldid=1328783440"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp