Module fibers::sync::mpsc [] [src]

Multi-producer, single-consumer FIFO queue communication primitives.

Basically, the structures in this module are thin wrapper of the standard mpsc module's counterparts. The former implement futures interface due to facilitate writing asynchronous processings and can wait efficiently on fibers until an event of interest happens.

Examples

use fibers::{Executor, InPlaceExecutor, Spawn};
use fibers::sync::mpsc;
use futures::{Future, Stream};

let mut executor = InPlaceExecutor::new().unwrap();
let (tx0, rx) = mpsc::channel();

// Spanws receiver
let mut monitor = executor.spawn_monitor(rx.for_each(|m| {
    println!("# Recv: {:?}", m);
    Ok(())
}));

// Spawns sender
let tx1 = tx0.clone();
executor.spawn_fn(move || {
    tx1.send(1).unwrap();
    tx1.send(2).unwrap();
    Ok(())
});

// It is allowed to send messages from the outside of a fiber.
// (The same is true of receiving)
tx0.send(0).unwrap();
std::mem::drop(tx0);

// Runs `executor` until the receiver exits (i.e., channel is disconnected)
while monitor.poll().unwrap().is_not_ready() {
    executor.run_once().unwrap();
}

Note

Unlike fibers::net module, the structures in this module can be used on both inside and outside of a fiber.

Implementation Details

If a receiver tries to receive a message from an empty channel, it will suspend (deschedule) current fiber by invoking the function. Then it writes data which means "I'm waiting on this fiber" to an object shared with the senders. If a corresponding sender finds there is a waiting receiver, it will resume (reschedule) the fiber, after sending a message.

Structs

Receiver

The receiving-half of a mpsc channel.

Sender

The sending-half of a asynchronous channel.

SyncSender

The sending-half of a synchronous channel.

Functions

channel

Creates a new asynchronous channel, returning the sender/receiver halves.

sync_channel

Creates a new synchronous, bounded channel.