How to call await on a MutexGuard?

let tx = tx.lock().unwrap();
if tx.await.is_err() {
    println!("send err");
    return Ok(());
}

I have some codes like this, it reports the trait Future is not implemented for `std::sync::MutexGuard<'_, FutureTx>, while FutureTx has been implemented Future.

The reason for the particular error you got is that .await requires taking ownership of the value, but you don't have ownership — only a mutable borrow.

However even if you fix this, you will probably run into the error described here. A blocking mutex may not be locked during a use of .await.

Hi thanks for explaining, I met a problem that await on a mutex while pending, the mutex would lock forever, how to solve it?

Either don't do that, or use the Mutex from Tokio.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.