Mutex

Struct Mutex 

Source
pub struct Mutex<T> { /* private fields */ }
Expand description

An OptionLock with a guaranteed value.

Implementations§

Source§

impl<T> Mutex<T>

Source

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}
Source

pub fn is_locked(&self) -> bool

Check if a guard is held.

Source

pub fn is_poisoned(&self) -> bool

Check if the contained value was removed.

Source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the contained value

Source

pub fn into_inner(self) -> Result<T, PoisonError>

Unwrap an owned mutex instance.

Source

pub fn try_lock(&self) -> Result<MutexGuard<'_, T>, MutexLockError>

Try to acquire an exclusive lock around the contained value

Source

pub fn try_lock_arc( self: &Arc<Self>, ) -> Result<MutexGuardArc<T>, MutexLockError>

Try to acquire an exclusive lock for an Arc<Mutex>.

Source

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: Clone> Mutex<T>

Source

pub fn try_clone(&self) -> Result<T, MutexLockError>

Try to clone the contained resource.

Source§

impl<T: Copy> Mutex<T>

Source

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§

Source§

impl<T> Debug for Mutex<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> RefUnwindSafe for Mutex<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.