Crate comm[stability] [-] [+] [src]

Communication primitives.

This library provides types for message passing between threads and polling. Concretely, it provides

channels of different flavors and a Select object which can poll the consuming ends of these channels for readiness.

Examples

Simple usage:

use std::{thread};
use comm::{spsc};

// Create a bounded SPSC channel.
let (send, recv) = spsc::bounded::new(10);
thread::spawn(move || {
    send.send_sync(10).unwrap();
});
assert_eq!(recv.recv_sync().unwrap(), 10);

Shared usage:

use std::{thread};
use comm::{mpsc};

// Create an unbounded MPSC channel.
let (send, recv) = mpsc::unbounded::new();
for i in 0..10 {
    let send = send.clone();
    thread::spawn(move || {
        send.send(i).unwrap();
    });
}
drop(send);
while let Ok(n) = recv.recv_sync() {
    println!("{}", n);
}

Selecting:

use std::{thread};
use std::old_io::{timer};
use std::time::duration::{Duration};
use comm::{spsc};
use comm::select::{Select, Selectable};

let mut channels = vec!();
for i in 0..10 {
    let (send, recv) = spsc::one_space::new();
    channels.push(recv);
    thread::spawn(move || {
        timer::sleep(Duration::milliseconds(100));
        send.send(i).ok();
    });
}
let select = Select::new();
for recv in &channels {
    select.add(recv);
}
let first_ready = select.wait(&mut [0])[0];
for recv in &channels {
    if first_ready == recv.id() {
        println!("First ready: {}", recv.recv_sync().unwrap());
        return;
    }
}

Modules

arc

Fork of the arc module in the rust stdlib.

mpmc

Multiple-producers multiple-consumers (MPMC) channels.

mpsc

Multiple-producers single-consumer (MPSC) channels.

select

A structure for polling channels and other objects.

spmc

Single-producer multiple-consumers (SPMC) channels.

spsc

Single-producer single-consumer (SPSC) channels.

Enums

Error

Errors that can happen during receiving and sending.

Traits

Sendable

Types able to be transferred across thread boundaries.