pub struct Mutex<T: ?Sized, R = Spin> { /* private fields */ }
mutex
only.Expand description
A spin-based lock providing mutually exclusive access to data.
The implementation uses either a ticket mutex or a regular spin mutex depending on whether the spin_mutex
or
ticket_mutex
feature flag is enabled.
§Example
use spin;
let lock = spin::Mutex::new(0);
// Modify the data
*lock.lock() = 2;
// Read the data
let answer = *lock.lock();
assert_eq!(answer, 2);
§Thread safety example
use spin;
use std::sync::{Arc, Barrier};
let thread_count = 1000;
let spin_mutex = Arc::new(spin::Mutex::new(0));
// We use a barrier to ensure the readout happens after all writing
let barrier = Arc::new(Barrier::new(thread_count + 1));
for _ in (0..thread_count) {
let my_barrier = barrier.clone();
let my_lock = spin_mutex.clone();
std::thread::spawn(move || {
let mut guard = my_lock.lock();
*guard += 1;
// Release the lock to prevent a deadlock
drop(guard);
my_barrier.wait();
});
}
barrier.wait();
let answer = { *spin_mutex.lock() };
assert_eq!(answer, thread_count);
Implementations§
source§impl<T, R> Mutex<T, R>
impl<T, R> Mutex<T, R>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
source§impl<T: ?Sized, R: RelaxStrategy> Mutex<T, R>
impl<T: ?Sized, R: RelaxStrategy> Mutex<T, R>
sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Locks the Mutex
and returns a guard that permits access to the inner data.
The returned value may be dereferenced for data access and the lock will be dropped when the guard falls out of scope.
let lock = spin::Mutex::new(0);
{
let mut data = lock.lock();
// The lock is now locked and the data can be accessed
*data += 1;
// The lock is implicitly dropped at the end of the scope
}
source§impl<T: ?Sized, R> Mutex<T, R>
impl<T: ?Sized, R> Mutex<T, R>
sourcepub fn is_locked(&self) -> bool
pub fn is_locked(&self) -> bool
Returns true
if the lock is currently held.
§Safety
This function provides no synchronization guarantees and so its result should be considered ‘out of date’ the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.
sourcepub unsafe fn force_unlock(&self)
pub unsafe fn force_unlock(&self)
sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data.
Since this call borrows the Mutex
mutably, and a mutable reference is guaranteed to be exclusive in Rust,
no actual locking needs to take place – the mutable borrow statically guarantees no locks exist. As such,
this is a ‘zero-cost’ operation.
§Example
let mut lock = spin::Mutex::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.lock(), 10);
Trait Implementations§
source§impl<R: RelaxStrategy> RawMutex for Mutex<(), R>
impl<R: RelaxStrategy> RawMutex for Mutex<(), R>
§type GuardMarker = GuardSend
type GuardMarker = GuardSend
Send
. Use
one of the GuardSend
or GuardNoSend
helper types here.