如何创建一个带有互斥锁的异步递归函数? Rust声称这段代码在await点(即暂停执行处)保留了互斥锁,然而该值在
.await
之前被释放。#[async_recursion]
async fn f(mutex: &Arc<Mutex<u128>>) {
let mut unwrapped = mutex.lock().unwrap();
*unwrapped += 1;
let value = *unwrapped;
drop(unwrapped);
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
if value < 100 {
f(mutex);
}
}
错误
future cannot be sent between threads safely
within `impl futures::Future<Output = ()>`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, u128>`
required for the cast to the object type `dyn futures::Future<Output = ()> + std::marker::Send`rustc
lib.rs(251, 65): future is not `Send` as this value is used across an await
?Send
。虽然第二种方式并不危险,但编译器会在您尝试发送无法在线程之间发送且未实现Send
特性的类型时停止您。 - smitopdrop
没有达到同样的效果呢? - Testdrop(unwrapped)
实际上是否丢弃了unwrapped
,而不是可能将其发送到另一个线程,在await
点之后再使用它(drop
只是一个普通函数)。 - smitopdrop
是一个普通函数,但它会移动值并且编译器知道这一点。对代码进行一些小的调整表明它是async_recursion
使用的生命周期,但我不确定。在异步函数中跟踪 Drop 的方式被认为过于保守。 - Chayim Friedman