Struct Mutex

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

A mutex grants synchronized access to a value

§Example

use syncrs::Mutex;
use std::sync::Arc;

let n = Arc::new(Mutex::new(0));

let threads = (0..10).map(|_|{
    let c = Arc::clone(&n);
    std::thread::spawn(move || {
        let mut sync_n = c.lock();
        for _ in 0..10 {
            *sync_n += 1;
        }
    })
}).collect::<Vec<_>>();

for t in threads {
    t.join().unwrap();
}

assert_eq!(*n.lock(), 100);

Implementations§

Source§

impl<T> Mutex<T>

Source

pub const fn new(val: T) -> Self

Creates a new Mutex from the given value

Source

pub fn into_inner(self) -> T

Consumes self and returns the inner T value

§Safety

Since we take self by value, we don’t need to synchronize the access.

Source§

impl<T: ?Sized> Mutex<T>

Source

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

Returns a lock guard for self, if it is available. If the operation whould’ve locked the execution, returns None inmediately

The lock is held until the MutexGuard is dropped.

§Example
use syncrs::Mutex;

let mutex = Mutex::new(10);
let guard1 = mutex.lock();

// guard1 holds the lock
assert_eq!(mutex.try_lock(), None);
drop(guard1); // This frees the mutex
assert!(mutex.try_lock().is_some());
Source

pub fn lock(&self) -> MutexGuard<'_, T>

Locks the mutex until it’s available, and returns a guard to the object.

The lock is held until the MutexGuard is dropped.

§Example
use syncrs::Mutex;

let mutex = Mutex::new(10);
let guard1 = mutex.lock();
assert_eq!(*guard1, 10);
Source

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

Gets a mutable reference to the object.

§Safety

Since this function gets a mutable self reference, we know the mutex is available. The reference is statically guaranteed to be unique, so we need not to worry about synchronization.

This is faster that spinlocking, and is 100% safe.

Source

pub fn is_locked(&self) -> bool

Returns true if self is currently locked

§Example
use syncrs::Mutex;

let mutex = Mutex::new(5);

{
    let guard = mutex.lock();
    assert!(mutex.is_locked());
} // guard is dropped here
assert!(!mutex.is_locked());

Trait Implementations§

Source§

impl<T: Default> Default for Mutex<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ?Sized + Send> Send for Mutex<T>

T must be Send for Mutex<T> to be Send It’s possible to unwrap the Mutex into it’s inner value (Mutex::into_inner). So we need to make sure that T is Send.

Source§

impl<T: ?Sized + Send> Sync for Mutex<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

impl<T> Unpin for Mutex<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for Mutex<T>
where T: UnwindSafe + ?Sized,

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.