Struct std::sync::StaticMutex [] [src]

pub struct StaticMutex {
    // some fields omitted
}
Unstable (static_mutex #27717)

: may be merged with Mutex in the future

The static mutex type is provided to allow for static allocation of mutexes.

Note that this is a separate type because using a Mutex correctly means that it needs to have a destructor run. In Rust, statics are not allowed to have destructors. As a result, a StaticMutex has one extra method when compared to a Mutex, a destroy method. This method is unsafe to call, and documentation can be found directly on the method.

Examples

#![feature(static_mutex)] fn main() { use std::sync::{StaticMutex, MUTEX_INIT}; static LOCK: StaticMutex = MUTEX_INIT; { let _g = LOCK.lock().unwrap(); // do some productive work } // lock is unlocked here. }
#![feature(static_mutex)]

use std::sync::{StaticMutex, MUTEX_INIT};

static LOCK: StaticMutex = MUTEX_INIT;

{
    let _g = LOCK.lock().unwrap();
    // do some productive work
}
// lock is unlocked here.

Methods

impl StaticMutex

const fn new() -> StaticMutex

Unstable (static_mutex #27717)

: may be merged with Mutex in the future

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

fn lock(&'static self) -> LockResult<MutexGuard<()>>

Unstable (static_mutex #27717)

: may be merged with Mutex in the future

Acquires this lock, see Mutex::lock

fn try_lock(&'static self) -> TryLockResult<MutexGuard<()>>

Unstable (static_mutex #27717)

: may be merged with Mutex in the future

Attempts to grab this lock, see Mutex::try_lock

unsafe fn destroy(&'static self)

Unstable (static_mutex #27717)

: may be merged with Mutex in the future

Deallocates resources associated with this static mutex.

This method is unsafe because it provides no guarantees that there are no active users of this mutex, and safety is not guaranteed if there are active users of this mutex.

This method is required to ensure that there are no memory leaks on all platforms. It may be the case that some platforms do not leak memory if this method is not called, but this is not guaranteed to be true on all platforms.