#channel #multi-consumer #fifo-queue

shared_channel

Multi-producer, multi-consumer FIFO queue communication primitives

2 unstable releases

Uses old Rust 2015

0.2.0 Oct 29, 2018
0.1.0 Oct 28, 2018

#841 in Concurrency

MIT license

18KB
469 lines

shared channel

This is a implementation of multi-producer, multi-consumer channel in Rust.

Example

simple usage

use std::thread;

extern crate shared_channel;
use shared_channel::shared_channel;

fn main() {
    let (tx, rx) = shared_channel();
    for i in 0..10 {
        let rx = rx.clone();
        thread::spawn(move || println!("{}", rx.recv().unwrap()));
    }
    for i in 0..10 {
        tx.send(i).unwrap();
    }
}

Pre-forked web server

git clone https://github.com/hinohi/rust_shared_channel.git
cd rust_shared_channel
cargo run --example hello_server

Please access http://localhost:5000. Its response looks like Hello! I'm No.0. The number shows ID of thread-pool's worker.

TODO

  • implement Reciver's stable API
    • try_recv
    • recv
    • recv_timeout
    • iter
    • try_iter
  • implement shared_sync_channel

lib.rs:

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

This module is extension of std::sync::mpsc, almost has same API with it. Differences are:

  • A struct SharedReceiver is defined. This is clone-able struct (multi-consumer).
  • A function shared_channel corresponding to function channel is defined. shared_channel returns a (Sender, SharedReceiver) tuple instead of (Sender, Receiver) tuple. Sender is a struct that defined at std::sync::mpsc.
  • A function shared_sync_channel corresponding to function sync_channel is also defined.
  • Some feature of std::sync::mpsc is not implemented yet, for example recv_timeout.

Example

Simple usage:

let (tx, rx) = shared_channel();
for i in 0..10 {
    let rx = rx.clone();
    thread::spawn(move || println!("{}", rx.recv().unwrap()));
}

for i in 0..10 {
    tx.send(i).unwrap();
}

More examples, see examples directory.

No runtime deps