Giter Site home page Giter Site logo

spmc's Introduction

spmc

Build Status crates.io

Single-Producer, Multiple-Consumer channel for Rust.

Documentation

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

spmc's People

Contributors

seanmonstar avatar atouchet avatar jonhoo avatar bash avatar siddontang avatar

Stargazers

Chris Marshall avatar Ed Cashin avatar Gleb Chipiga avatar  avatar Tianyuan Sun avatar Thelonious Cooper avatar Naoki Ikeguchi avatar Jon avatar Pavlos Karakalidis avatar Gihwan Oh avatar John Wass avatar  avatar Nick Spain avatar Mingwei Zhang avatar ypcpy avatar sen ✦ avatar Nikita avatar xixi avatar GuokeCui avatar Sam L. Thomas avatar Nishanth Shanmugham avatar GAURAV avatar Yasha avatar Pierre Lafievre avatar Julien Tregoat avatar Filippo Cucchetto avatar  avatar Graeme Coupar avatar Kornelijus avatar Soo Philip Jason Kim avatar LingFeng avatar Jan Vandepitte avatar krircc avatar Robin avatar Petrus Theron avatar Christoph Grabo avatar Nikolay Koshenkov avatar Juri Hahn avatar A ghost. avatar  avatar Trung Pham avatar Sathya Narrayanan avatar  avatar Lorenzo Veronese avatar Dean avatar Nelson Chen avatar Zverev Konstantin avatar Ivan Ivashchenko avatar Christoph Herzog avatar Vlad Frolov avatar Ilia Averianov avatar Максим Сохацький avatar Markus Kohlhase avatar Vinh Quốc Nguyễn avatar Arjan Topolovec avatar Fedor Gogolev avatar

Watchers

 avatar Fedor Gogolev avatar James Cloos avatar  avatar

spmc's Issues

Segmentation fault when using spmc:::channel

After a few days studying Rust I have come to a road block with segmentation faults and other errors when running a simple program using an spmc channel.

use std::thread;
use std::fs::File;
use std::io::{BufReader,BufRead};

pub fn main() {
    let (tx, rx) = spmc::channel();

    let mut handles = Vec::new();
    for n in 0..3 {
        let rx = rx.clone();
        handles.push(thread::spawn(move || {
            let mut word_count = 0;
            loop {
                let received = rx.recv();
                match received {
                    Ok(_word) => {
                        word_count = word_count + 1;
                    },
                    Err(_e) => {
                        println!("Reader {}: {}, ", n, word_count);
                        break;
                    }
                }
            }
        }));
    }

    handles.push(thread::spawn(move || {
        let file = File::open("/usr/share/dict/british-english-insane").unwrap();
        for line in BufReader::new(file).lines() {
            let word = line.unwrap();
            tx.send(word).unwrap();
        }
    }));

    for handle in handles {
        handle.join().unwrap();
    }

    println!("Done.");
}

This code should simply read all the lines of a Debian dictionary file and forward them to four threads. The expected result is like:

$ ./target/debug/threads-rust
Reader 2: 221485,
Reader 1: 217272,
Reader 0: 215518,
Done.

But mostly I get seg faults and other errors like so:

$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
    Running `target/debug/threads-rust`
munmap_chunk(): invalid pointer
Aborted (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
    Running `target/debug/threads-rust`
Segmentation fault (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
    Running `target/debug/threads-rust`
Reader 0: 654276,
Done.
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
    Running `target/debug/threads-rust`
munmap_chunk(): invalid pointer
Aborted (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
    Running `target/debug/threads-rust`
free(): invalid size
Aborted (core dumped)
$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
    Running `target/debug/threads-rust`
double free or corruption (out)
Aborted (core dumped)

I get similar errors on a Raspberry Pi 3.

$ uname -a
Linux monster 4.4.0-17134-Microsoft #706-Microsoft Mon Apr 01 18:13:00 PST 2019 x86_64 GNU/Linux
$ cargo --version
cargo 1.36.0 (c4fcfb725 2019-05-15)
$ rustc --version --verbose
rustc 1.36.0 (a53f9df32 2019-07-03)
binary: rustc
commit-hash: a53f9df32fbb0b5f4382caaad8f1a46f36ea887c
commit-date: 2019-07-03
host: x86_64-unknown-linux-gnu
release: 1.36.0
LLVM version: 8.0    $ cargo  build
Compiling threads-rust v0.1.0 (/mnt/c/Users/zicog/conveqs/threads-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.72s

support sync_channel

Thanks for your nice work! It would be more powerful if spmc supported sync_channel!

Memory leak

Hi, I have a problem with memory leaks with your library. Data are not removed from memory after receiving from spmc rx channel. Simple example:

extern crate spmc;

use std::io;

fn main() {
    let (tx, rx) = spmc::channel();

    for i in 0..1000000 {
        tx.send(format!("Ahoj svete {}", i)).unwrap();
    }
    drop(tx);

    // Memory usage at this location: ~62 MB
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();

    loop {
        match rx.recv() {
            Ok(_) => continue,
            Err(_) => break,
        }
    }

    // Memory usage at this location: ~62 MB
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
}

Deadlock?

If I run the following loop, with 1 sender and 5 receiver, it happens that 1 or more threads never manage to finish.

extern crate spmc;

use std::thread;

fn main() {
    for l in 0..10 {
        println!("loop {}", l);

        let (tx, rx) = spmc::channel();

        let mut handles = Vec::new();
        for _ in 0..5 {
            let rx = rx.clone();
            handles.push(thread::spawn(move || {
                loop {
                    match rx.recv() {
                        Ok(_) => continue,
                        Err(_) => break,
                    }
                }
            }));
        }

        for i in 0..10 {
            tx.send(format!("Sending value {} {}", l, i)).unwrap();
        }
        drop(tx);

        for handle in handles {
            handle.join().unwrap();
        }
    }
}

The TX part is dropped, so all RX threads should finish quickly... There seems to be some kind of fundamental locking issue.

Futures support?

It'd be nice to be able to use a Receiver as a Stream. (I might take a swing at implementing this at some point.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.