Hello community,
I'm trying to make a pool of thread workers:
use crossbeam::channel::unbounded;
use crossbeam::thread;
fn main() {
let (sender, receiver) = unbounded();
let workers_count = 2;
let job_count = 4;
let mut current_cpu_i = 0;
thread::scope(|scope| {
for i in 0..job_count {
// Enter here when count of spawned thread is equal to worker
// capacity or when no more job to do
if current_cpu_i == workers_count || i == job_count - 1 {
for received in &receiver {
println!("Got: {}", received);
}
current_cpu_i = 0;
// Enter here to spawn thread worker
} else {
let thread_sender = sender.clone();
current_cpu_i += 1;
scope.spawn(move |_| {
thread_sender.send(i).unwrap();
});
}
}
})
.unwrap();
println!("ok")
}
Execute this code result infinite wait into for received in &receiver {:
Got: 0
Got: 1
In this discussion it appears than we must drop the sender object. But if i try to drop it before for received in &receiver {, it can't work because sender will be used more later:
error[E0382]: use of moved value: `sender`
--> src/main.rs:15:22
|
15 | drop(sender);
| ^^^^^^ value moved here, in previous iteration of loop
|
= note: move occurs because `sender` has type `crossbeam_channel::channel::Sender<i32>`, which does not implement the `Copy` trait
How to deal with this problematic ? Or maybe, there is a easier way to use a worker pool with crossbeam ?