我有以下代碼:如何刪除傳遞給futures-cpupool的封閉環境?
extern crate futures;
extern crate futures_cpupool;
extern crate tokio_timer;
use std::time::Duration;
use futures::Future;
use futures_cpupool::CpuPool;
use tokio_timer::Timer;
fn work(foo: Foo) {
std::thread::sleep(std::time::Duration::from_secs(10));
}
#[derive(Debug)]
struct Foo { }
impl Drop for Foo {
fn drop(&mut self) {
println!("Dropping Foo");
}
}
fn main() {
let pool = CpuPool::new_num_cpus();
let foo = Foo { };
let work_future = pool.spawn_fn(|| {
let work = work(foo);
let res: Result<(),()> = Ok(work);
res
});
println!("Created the future");
let timer = Timer::default();
let timeout = timer.sleep(Duration::from_millis(750))
.then(|_| Err(()));
let select = timeout.select(work_future).map(|(win, _)| win);
match select.wait() {
Ok(()) => { },
Err(_) => { },
}
}
看來這個代碼不執行Foo::drop
- 打印任何消息。
我預計foo
一旦timeout
未來解決在select
,因爲它是一個關閉的環境的一部分,傳遞到下降的未來。
如何使其執行Foo::drop
?
它看起來像'foo'正在一個線程中使用,超過程序終止。參看https://users.rust-lang.org/t/stopping-a-thread/6328。我不知道是否http://stackoverflow.com/questions/26199926/how-to-terminate-or-suspend-a-rust-thread-from-another-thread是否足以回答你的問題? – ArtemGr