I just want to resue buffer
after it be send by channel, becasue buffer.to_owned()
is expensive when iterated tens of thousands of times.
Here is the simplified code:
use crossbeam_channel::bounded;
use std::thread;
fn main() {
thread::scope(|work| {
let batch_size = 100000000;
let (in_s, in_r) = bounded(1);
work.spawn(move || {
let mut buffer = Vec::with_capacity(batch_size);
for i in 0..10000{
buffer.extend(std::iter::repeat(4).take(batch_size));//read data from file
in_s.send(buffer.to_owned()).unwrap();
buffer.clear();
}
});
work.spawn(move || {
while let Ok(data) = in_r.recv() {
// do some caculation
}
});
});
}
So how can I modify the above code to reuse the buffer
?
I have tried to resend buffer
in the receiving thread to the sending thread, but failed.