I want to implement an executor. Here I should store the thread handle in a struct then I can join it for waiting threads to stop friendly. But it errors when I prepare to invoke the join() method. I would like to know the reason and how to fix that. The codes are below:
struct Executor{
tx:Sender<Box<dyn Send + Fn()>>,
t:JoinHandle<()>,
}
impl Executor {
fn new()->Self{
let (tx, rx) = mpsc::channel::<Box<dyn Send + Fn()>>();
let handle = thread::spawn(move || {
loop{
//TODO: check stop flag
rx.recv().unwrap()();
}
});
let ins = Executor{tx:tx, t:handle};
ins
}
fn sender(&self)->Sender<Box<dyn Send + Fn()>>{
self.tx.clone()
}
fn join(&self){
self.t.join().unwrap();
}
}
the errors:
error[E0507]: cannot move out of `self.t` which is behind a shared reference
--> src\main.rs:77:9
|
77 | self.t.join().unwrap();
| ^^^^^^ move occurs because `self.t` has type `JoinHandle<()>`, which does not implement the `Copy` trait