dpc / mioco.pre-0.9

Scalable, coroutine-based, asynchronous IO handling library for Rust programming language. (aka MIO COroutines).
Mozilla Public License 2.0
457 stars 30 forks source link

Weird behavior with the scheduler #80

Closed jedisct1 closed 8 years ago

jedisct1 commented 8 years ago

I'm trying to get multiple consumers for a single producer

extern crate mioco;

use std::sync::Arc;
use mioco::mail;
use mioco::sync::Mutex;

fn main() {
    mioco::start_threads(5, move || {
        let (boxout, boxin) = mail::mailbox::<u64>();
        mioco::spawn(move || {
            let mut x = 0u64;
            loop {
                boxout.send(x);
                x = x + 1;
                mioco::sleep(500);
            }
        });

        let xboxin = Arc::new(Mutex::new(boxin));
        let xboxin2 = xboxin.clone();
        mioco::spawn(move || {
            loop {
                let x = { xboxin.lock().unwrap().read() };
                println!("{}\tA", x);
                mioco::sleep(2500);
            }
        });

        mioco::spawn(move || {
            loop {
                let x = { xboxin2.lock().unwrap().read() };
                println!("{}\tB", x);
                mioco::sleep(500);
            }
        });
        Ok(())
    });
}

This is what happens:

0   A
1   B
2   B
3   B
4   B
5   A
6   A
7   A
8   A
9   A
10  A
11  A
12  A
13  A
14  A
15  A
16  A
17  A
18  A
19  A
20  A
...

And it goes on and on, only A is printed from now on.

Everything quickly gets consumer by the first consumer coroutine, and only it. Nothing gets consumed by the second one any more. A is the slow coroutine, which is sleeping for 2500ms, while B is yielding faster (500ms), so shouldn't the opposite happen?

dpc commented 8 years ago

I think this is a bug. I think mailbox was supposed to be mpsc, and Receiving end not Clone-able, but I need to double check that.

I understand that for this scenario you'd like mpmc queue. Another thing to address in #76 .

Thanks for reporting this!

dpc commented 8 years ago

Oh, I've noticed that you're using mioco::sync::Mutex. I've tried locally with current master, and it workd just fine.

dpc commented 8 years ago

I'm unable to reproduce. Maybe it was .. hmm... timing issue. Depends on how many cores you have or something. I'm going to close it, but if you have some followup, feel free to re-open.