pub struct Mutex<T: ?Sized> { /* private fields */ }
Expand description
Mutex implementation to use in conjunction with MaybeSync
bound.
A type alias to parking_lot::Mutex
when “sync” feature is enabled.
A wrapper type around std::cell::RefCell
when “sync” feature is not enabled.
§Example
fn maybe_sends<T: MaybeSend + Debug + 'static>(val: Arc<Mutex<T>>) {
#[cfg(feature = "sync")]
{
// If this code is compiled then `MaybeSend` is alias to `std::marker::Send`,
// and `Mutex` is `parking_lot::Mutex`.
std::thread::spawn(move || { println!("{:?}", *val.lock()) });
}
}
// `maybe_sync::Mutex<T>` would always satisfy `MaybeSync` and `MaybeSend`
// bounds when `T: MaybeSend`,
// even if feature "sync" is enabeld.
maybe_sends(Arc::new(Mutex::new(42)));
Implementations§
Source§impl<T> Mutex<T>where
T: ?Sized,
impl<T> Mutex<T>where
T: ?Sized,
Sourcepub fn lock(&self) -> RefMut<'_, T>
pub fn lock(&self) -> RefMut<'_, T>
Acquires a mutex, blocking the current thread until it is able to do so.
This function will block the local thread until it is available to acquire the mutex.
Upon returning, the thread is the only thread with the mutex held.
An RAII guard is returned to allow scoped unlock of the lock.
When the guard goes out of scope, the mutex will be unlocked.
Attempts to lock a mutex in the thread which already holds the lock will result in a deadlock.
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Send for Mutex<T>
impl<T> !Sync for Mutex<T>
impl<T> Unpin for Mutex<T>
impl<T> UnwindSafe for Mutex<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more