MutexGuard 与 .await 问题
zydxhs opened this issue · 1 comments
zydxhs commented
文件:doc/SharedState.md
注意下面这种无法工作:
use std::sync::Mutex;
// 这也会失败
async fn increment_and_do_stuff(mutex: &Mutex<i32>) {
let mut lock = mutex.lock().unwrap();
*lock += 1;
drop(lock);
do_something_async().await;
}
经过验证,上面的代码可以正常编译。
编译环境:win10 x64 + rust v1.47
dslchd commented
Hello! 因为Rust 异步fn , future是惰性的,单独写,并编译并无问题,如果放到运行时中执行,就不行,试试 tokio::spawn
let my_mutex = Mutex::new(1);
tokio::spawn(async move {
increment_and_do_stuff(&my_mutex).await;
});