Struct TryLock
struct TryLock<T> { ... }
A light-weight lock guarded by an atomic boolean.
Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap.
It is only possible to try to acquire the lock, it is not possible to
wait for the lock to become ready, like with a Mutex.
Implementations
impl<T> TryLock<T>
const fn new(val: T) -> TryLock<T>Create a
TryLockaround the value.fn try_lock(self: &Self) -> Option<Locked<'_, T>>Try to acquire the lock of this value.
If the lock is already acquired by someone else, this returns
None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.Note
The default memory ordering is to use
Acquireto lock, andReleaseto unlock. If different ordering is required, usetry_lock_explicitortry_lock_explicit_unchecked.fn try_lock_order(self: &Self, lock_order: Ordering, unlock_order: Ordering) -> Option<Locked<'_, T>>Try to acquire the lock of this value using the lock and unlock orderings.
If the lock is already acquired by someone else, this returns
None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.fn try_lock_explicit(self: &Self, lock_order: Ordering, unlock_order: Ordering) -> Option<Locked<'_, T>>Try to acquire the lock of this value using the specified lock and unlock orderings.
If the lock is already acquired by someone else, this returns
None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.Panic
This method panics if
lock_orderis not any ofAcquire,AcqRel, andSeqCst, orunlock_orderis not any ofReleaseandSeqCst.unsafe fn try_lock_explicit_unchecked(self: &Self, lock_order: Ordering, unlock_order: Ordering) -> Option<Locked<'_, T>>Try to acquire the lock of this value using the specified lock and unlock orderings without checking that the specified orderings are strong enough to be safe.
If the lock is already acquired by someone else, this returns
None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.Safety
Unlike
try_lock_explicit, this method is unsafe because it does not check that the given memory orderings are strong enough to prevent data race.fn into_inner(self: Self) -> TTake the value back out of the lock when this is the sole owner.
impl<T> Any for TryLock<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for TryLock<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for TryLock<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Freeze for TryLock<T>
impl<T> From for TryLock<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for TryLock<T>
impl<T> Unpin for TryLock<T>
impl<T> UnsafeUnpin for TryLock<T>
impl<T> UnwindSafe for TryLock<T>
impl<T, U> Into for TryLock<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for TryLock<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for TryLock<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: $crate::default::Default> Default for TryLock<T>
fn default() -> TryLock<T>
impl<T: Send> Send for TryLock<T>
impl<T: Send> Sync for TryLock<T>
impl<T: fmt::Debug> Debug for TryLock<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result