Rustで安全にthread loopを止める方法
Rustの無限ループで安全にloop処理から抜ける方法は
どの様に書けば良いですか。
use std::{thread, time};
pub struct Worker { }
trait WorkerTrait {
fn run(&self);
fn stop(&self);
}
impl Worker {
fn run(&self) {
thread::spawn(move || loop {
println!("worker1 working ");
thread::sleep(time::Duration::from_millis(1000)); // 擬似処理.
// thread break?
});
}
fn stop(&self) {
// to break thread.
}
}
fn main() {
let w = Worker {};
w.run();
thread::sleep(time::Duration::from_secs(10));
// stop thread.
w.stop();
}