Dear Stackoverflow community,
I just started to play around with rust. To increase speed, I took a look at crossbeam: The content of the channel should be processed by several thread at the same time. On the one hand a wait condition for the threads is missing (instant termination) and the parallel processing is not working as expected. Here is a small example:
use std::thread::sleep;
use std::time::Duration;
use crossbeam_utils::thread;
use crossbeam_utils::thread::scope;
// heavy processing
fn process(s:&str) {
println!("receive: {:?}", s);
sleep(Duration::from_secs(3));
}
fn main() {
let files_to_process = vec!["file1.csv", "file2.csv", "file3.csv", "file4.csv", "file5.csv"];
let (s, r) = crossbeam::channel::unbounded();
for e in files_to_process {
println!("sending: {:?}", e);
s.send(e).unwrap()
}
drop(s);
let clonned_r = r.clone();
scope(|scope| {
for _ in [0..3] {
scope.spawn(|_| {
match clonned_r.recv() {
Ok(s) => process(s),
_ => panic!("something went wrong")
}
});
}
}).unwrap();
}
Output:
sending: "file1.csv"
sending: "file2.csv"
sending: "file3.csv"
sending: "file4.csv"
sending: "file5.csv"
receive: "file1.csv"
Expected Output: Every "file" is displayed (and processed in parallel)
The documentation is missing an example how to setup several consumers.