Created
August 8, 2018 08:20
-
-
Save gterzian/44c672c75bc95dc89268f1477ba550bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note select is unstable, | |
// consider using crossbeam-channel instead... | |
#![feature(mpsc_select)] | |
loop { | |
let msg = { | |
let sel = Select::new(); | |
// Selecting over our "work receiver", | |
// corresponding to the sender shared with the "producer" | |
// And our "results receiver", corresponding | |
// with the sender shared with the "consumer". | |
// So we're either getting work, or results. | |
let mut work_port = sel.handle(&work_receiver); | |
let mut results_port = sel.handle(&results_receiver); | |
unsafe { | |
work_port.add(); | |
results_port.add(); | |
} | |
let ready = sel.wait(); | |
if ready == work_port.id() { | |
MainMsg::FromProducer(work_port.recv().unwrap()) | |
} else if ready == results_port.id() { | |
MainMsg::FromConsumer(results_port.recv().unwrap()) | |
} else { | |
panic!("unexpected select result") | |
} | |
}; | |
// Handling the "msg" below... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment