Skip to content

Instantly share code, notes, and snippets.

@gterzian
Last active August 7, 2018 22:01
Show Gist options
  • Select an option

  • Save gterzian/d9f34ffcdfd5b50895177f0392ae9e0f to your computer and use it in GitHub Desktop.

Select an option

Save gterzian/d9f34ffcdfd5b50895177f0392ae9e0f to your computer and use it in GitHub Desktop.
fn merge(merged_result_chan: Sender<PipelineMsg>) -> Sender<PipelineMsg> {
let (chan, port) = channel();
let _ = thread::Builder::new().spawn(move || {
// Iteration will stop when all senders are dropped.
// This happens when the worker stop their iteration,
// and drop their "merge_chan"...
for msg in port {
let squared = match msg {
PipelineMsg::Squared(num) => num,
_ => panic!("unexpected message receiving at merge stage"),
};
// Sending "merged results" back to the main thread...
let _ = merged_result_chan.send(PipelineMsg::Merged(squared));
}
});
// Sharing the "merge_chan" with the world...
chan
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment