Struct sync::mutex::Mutex
pub struct Mutex { // some fields omitted }
A mutual exclusion primitive useful for protecting shared data
This mutex is an implementation of a lock for all flavors of tasks which may be grabbing. A common problem with green threads is that they cannot grab locks (if they reschedule during the lock a contender could deadlock the system), but this mutex does not suffer this problem.
This mutex will properly block tasks waiting for the lock to become
available. The mutex can also be statically initialized or created via a
new
constructor.
Example
use sync::mutex::Mutex; let m = Mutex::new(); let guard = m.lock(); // do some work drop(guard); // unlock the lock
Methods
impl Mutex
fn new() -> Mutex
Creates a new mutex in an unlocked state ready for use.
fn try_lock<'a>(&'a self) -> Option<Guard<'a>>
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.
fn lock<'a>(&'a self) -> Guard<'a>
Acquires a mutex, blocking the current task until it is able to do so.
This function will block the local task until it is available to acquire the mutex. Upon returning, the task is the only task 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.