Is there a way to use atomic types in an asynchronous context instead of an asynchronous Mutex
or RwLock
? Can standard atomics be used as is in an asynchronous context?
Or, for example, is there an asynchronous equivalent of std::sync::atomic::AtomicUsize
with load
/ store
methods, which could replace something like tokio::sync::RwLock<usize>
with read().await
/ write().await
methods?
Yes, using Atomics in an async context is no problem.
Most of them are lock-free (=can't even block).
Even if you would block, it is still recommended to use normal, blocking synchronization primitives over async ones, unless you hold that lock during an
await
.For more info I quote the respective chapter of the tokio tutorial:
Note that this is of course only true if all the threads accessing said lock/atomic are also async worker threads. If you have external threads that could block the mutex, you have to consider waiting for that mutex a blocking operation and treat it as such.