scoped_threadpool::Pool::scoped(&mut self, <closure>)
returns a closure that impls FnOnce
which means you can only call it once. You had it inside a for
loop which is why the compiler kept giving you errors with confusing suggestions. Once you refactor the code to move the for
outside the call to scoped
then it compiles and works as expected:
use scoped_threadpool::Pool;
fn main() {
let max_workers = 1;
let somevalue = 1;
let mut pool = Pool::new(max_workers);
let mut ret = true;
let ret_ref = &mut ret;
for i in 0..somevalue {
pool.scoped(|scoped| {
scoped.execute(|| {
// do something
let success = true;
if success {
*ret_ref = false
}
});
});
}
if ret == true {
println!("ret stayed true");
} else {
// prints this
println!("ret was changed to false in the scoped thread");
}
}
playground
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…