Skip to content

Instantly share code, notes, and snippets.

@gterzian
Created August 8, 2018 08:20
Show Gist options
  • Save gterzian/44c672c75bc95dc89268f1477ba550bb to your computer and use it in GitHub Desktop.
Save gterzian/44c672c75bc95dc89268f1477ba550bb to your computer and use it in GitHub Desktop.
// 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