When to use async_std

I see that both std::sync and async_std::sync define Mutex and RwLock.
How do I know which to use?
Am I supposed to always use the versions in async_std when I have this:

#[async_std::main]
async fn main() -> ... {
  ...
}

The tokio documentation has nice explanation when to use the sync mutex and when to use the async one.

https://docs.rs/tokio/1.1.1/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use

1 Like

If I understand this correctly, the difference is that std::sync::Mutex cannot hold a lock across await points, but tokio::sync::Mutex can. The linked document says the async version is good for resources such as database connections, but is not needed for normal data such as a HashMap.

I'm not sure if the same is true for async_std::sync::Mutex. Its documentation doesn't describe this behavior. async_std::sync::Mutex - Rust

It would be great if there was a resource that provided guidance for choosing between the std::sync, async_std::sync, tokio::sync, and parking_lot versions of all of these locking mechanisms.

Use the guide on Tokio's docs to choose between blocking or async lock, then use either Tokio or async-std's lock if you need an async lock, choosing the same as the one you use as runtime, or std/parking_lot if you need a blocking lock.

The shared state chapter in Tokio's tutorial has more details.

3 Likes

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.