channel
fn channel() -> Result<(Sender, Receiver), ThreadError> Import
Source: use thread::{channel}; compiler/src/builtin_docs/thread.hy Creates an unbounded channel and returns its (Sender, Receiver) ends.
Values sent are copied (they must be sendable, see spawn) and received
in order. The channel has no capacity limit, so send never waits.
Returns#
Result<(Sender, Receiver), ThreadError>
Examples#
use thread::{channel, send, recv, spawn, join, Sender};
use tests::assert;
fn produce(Sender tx) -> int {
let _ = send(tx, 10);
let _ = send(tx, 20);
return 0;
}
fn main() {
let (tx, rx) = match channel() {
Result::Ok(pair) => pair,
Result::Err(_) => panic "channel failed",
};
let t = match spawn(produce, tx) {
Result::Ok(t) => t,
Result::Err(_) => panic "spawn failed",
};
let a = match recv(rx) { Result::Ok(v) => v, Result::Err(_) => 0 };
let b = match recv(rx) { Result::Ok(v) => v, Result::Err(_) => 0 };
let _ = join(t);
assert!(a + b == 30);
}
See also#
thread::spawnthread::sendjoin: Waits for the thread to finish and returns the value its function returned.detach: Lets the thread run to completion without being joined.recv: Takes the next value from the channel, waiting until one arrives.try_send: Sendsvaluewithout waiting.try_recv: Takes the next value if there is one, without waiting.close: Closes the channel: no more sends.