# [−][src]Struct lock_api::Mutex

    pub struct Mutex<R: RawMutex, T: ?Sized> { /* fields omitted */ }

A mutual exclusion primitive useful for protecting shared data

This mutex will block threads waiting for the lock to become available. The mutex can also be statically initialized or created via a `new` constructor. Each mutex has a type parameter which represents the data that it is protecting. The data can only be accessed through the RAII guards returned from `lock` and `try_lock`, which guarantees that the data is only ever accessed when the mutex is locked.

## Methods

### `impl<R: RawMutex, T> Mutex<R, T>`[src]

#### `pub fn new(val: T) -> Mutex<R, T>`[src]

Creates a new mutex in an unlocked state ready for use.

#### `pub fn into_inner(self) -> T`[src]

Consumes this mutex, returning the underlying data.

### `impl<R: RawMutex, T: ?Sized> Mutex<R, T>`[src]

#### `pub fn lock(&self) -> MutexGuard<R, T>`[src]

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.

#### `pub fn try_lock(&self) -> Option<MutexGuard<R, T>>`[src]

Attempts to acquire this lock.

If the lock could not be acquired at this time, then `None` is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

This function does not block.

#### `pub fn get_mut(&mut self) -> &mut T`[src]

Returns a mutable reference to the underlying data.

Since this call borrows the `Mutex` mutably, no actual locking needs to take place---the mutable borrow statically guarantees no locks exist.

#### `pub unsafe fn force_unlock(&self)`[src]

Forcibly unlocks the mutex.

This is useful when combined with `mem::forget` to hold a lock without the need to maintain a `MutexGuard` object alive, for example when dealing with FFI.

# Safety

This method must only be called if the current thread logically owns a `MutexGuard` but that guard has be discarded using `mem::forget`. Behavior is undefined if a mutex is unlocked when not locked.

#### `pub unsafe fn raw(&self) -> &R`[src]

Returns the underlying raw mutex object.

Note that you will most likely need to import the `RawMutex` trait from `lock_api` to be able to call functions on the raw mutex.

# Safety

This method is unsafe because it allows unlocking a mutex while still holding a reference to a `MutexGuard`.

### `impl<R: RawMutexFair, T: ?Sized> Mutex<R, T>`[src]

#### `pub unsafe fn force_unlock_fair(&self)`[src]

Forcibly unlocks the mutex using a fair unlock procotol.

This is useful when combined with `mem::forget` to hold a lock without the need to maintain a `MutexGuard` object alive, for example when dealing with FFI.

# Safety

This method must only be called if the current thread logically owns a `MutexGuard` but that guard has be discarded using `mem::forget`. Behavior is undefined if a mutex is unlocked when not locked.

### `impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T>`[src]

#### `pub fn try_lock_for(&self, timeout: R::Duration) -> Option<MutexGuard<R, T>>`[src]

Attempts to acquire this lock until a timeout is reached.

If the lock could not be acquired before the timeout expired, then `None` is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

#### `pub fn try_lock_until(&self, timeout: R::Instant) -> Option<MutexGuard<R, T>>`[src]

Attempts to acquire this lock until a timeout is reached.

If the lock could not be acquired before the timeout expired, then `None` is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

## Trait Implementations

### `impl<R: RawMutex, T> From<T> for Mutex<R, T>`[src]

#### `fn from(t: T) -> Mutex<R, T>`[src]

Performs the conversion.

### `impl<R: RawMutex, T: ?Sized + Debug> Debug for Mutex<R, T>`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

### `impl<R: RawMutex + Send, T: ?Sized + Send> Send for Mutex<R, T>`[src]

### `impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<R, T>`[src]

### `impl<R: RawMutex, T: ?Sized + Default> Default for Mutex<R, T>`[src]

#### `fn default() -> Mutex<R, T>`[src]

Returns the "default value" for a type. Read more

## Auto Trait Implementations

### `impl<R, T: ?Sized> Unpin for Mutex<R, T> where
R: Unpin,
T: Unpin,`

### `impl<R, T: ?Sized> UnwindSafe for Mutex<R, T> where
R: UnwindSafe,
T: UnwindSafe,`

### `impl<R, T> !RefUnwindSafe for Mutex<R, T>`

## Blanket Implementations

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

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

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

### `impl<T> Erased for T`[src]

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)