我有以下代码:如何删除传递给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