pub struct Mutex<T> { /* private fields */ }
Expand description
An OptionLock
with a guaranteed value.
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub const fn new(value: T) -> Self
pub const fn new(value: T) -> Self
Create a new mutex instance.
Examples found in repository?
examples/try-mutex.rs (line 8)
7fn main() {
8 let shared = Arc::new(Mutex::new(0i32));
9 let threads = 100;
10 for _ in 0..threads {
11 let shared = shared.clone();
12 thread::spawn(move || {
13 let mut guard = shared.spin_lock().unwrap();
14 *guard += 1;
15 });
16 }
17 loop {
18 if shared.try_copy() == Ok(threads) {
19 break;
20 }
21 spin_loop()
22 }
23 println!("Completed {} threads", threads);
24}
Sourcepub fn is_poisoned(&self) -> bool
pub fn is_poisoned(&self) -> bool
Check if the contained value was removed.
Sourcepub fn into_inner(self) -> Result<T, PoisonError>
pub fn into_inner(self) -> Result<T, PoisonError>
Unwrap an owned mutex instance.
Sourcepub fn try_lock(&self) -> Result<MutexGuard<'_, T>, MutexLockError>
pub fn try_lock(&self) -> Result<MutexGuard<'_, T>, MutexLockError>
Try to acquire an exclusive lock around the contained value
Sourcepub fn try_lock_arc(
self: &Arc<Self>,
) -> Result<MutexGuardArc<T>, MutexLockError>
pub fn try_lock_arc( self: &Arc<Self>, ) -> Result<MutexGuardArc<T>, MutexLockError>
Try to acquire an exclusive lock for an Arc<Mutex>
.
Sourcepub fn spin_lock(&self) -> Result<MutexGuard<'_, T>, PoisonError>
pub fn spin_lock(&self) -> Result<MutexGuard<'_, T>, PoisonError>
In a spin loop, wait to acquire the mutex.
Examples found in repository?
examples/try-mutex.rs (line 13)
7fn main() {
8 let shared = Arc::new(Mutex::new(0i32));
9 let threads = 100;
10 for _ in 0..threads {
11 let shared = shared.clone();
12 thread::spawn(move || {
13 let mut guard = shared.spin_lock().unwrap();
14 *guard += 1;
15 });
16 }
17 loop {
18 if shared.try_copy() == Ok(threads) {
19 break;
20 }
21 spin_loop()
22 }
23 println!("Completed {} threads", threads);
24}
Source§impl<T: Copy> Mutex<T>
impl<T: Copy> Mutex<T>
Sourcepub fn try_copy(&self) -> Result<T, MutexLockError>
pub fn try_copy(&self) -> Result<T, MutexLockError>
Try to copy the contained resource.
On successful acquisition Some(T)
is returned. If the lock
is currently held or the value is empty, then None
is returned.
Examples found in repository?
examples/try-mutex.rs (line 18)
7fn main() {
8 let shared = Arc::new(Mutex::new(0i32));
9 let threads = 100;
10 for _ in 0..threads {
11 let shared = shared.clone();
12 thread::spawn(move || {
13 let mut guard = shared.spin_lock().unwrap();
14 *guard += 1;
15 });
16 }
17 loop {
18 if shared.try_copy() == Ok(threads) {
19 break;
20 }
21 spin_loop()
22 }
23 println!("Completed {} threads", threads);
24}
Trait Implementations§
impl<T> RefUnwindSafe for Mutex<T>
impl<T> UnwindSafe for Mutex<T>
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> Send for Mutex<T>where
T: Send,
impl<T> Sync for Mutex<T>where
T: Send,
impl<T> Unpin for Mutex<T>where
T: Unpin,
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