Struct std::sync::MutexExperimental
[-]
[+]
[src]
pub struct Mutex<T> {
// some 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.
Poisoning
In order to prevent access to otherwise invalid data, each mutex will propagate any panics which occur while the lock is held. Once a thread has panicked while holding the lock, then all other threads will immediately panic as well once they hold the lock.
Example
fn main() { use std::sync::{Arc, Mutex}; const N: uint = 10; // Spawn a few threads to increment a shared variable (non-atomically), and // let the main thread know once all increments are done. // // Here we're using an Arc to share memory among tasks, and the data inside // the Arc is protected with a mutex. let data = Arc::new(Mutex::new(0)); let (tx, rx) = channel(); for _ in range(0u, 10) { let (data, tx) = (data.clone(), tx.clone()); spawn(proc() { // The shared static can only be accessed once the lock is held. // Our non-atomic increment is safe because we're the only thread // which can access the shared state when the lock is held. let mut data = data.lock(); *data += 1; if *data == N { tx.send(()); } // the lock is unlocked here when `data` goes out of scope. }); } rx.recv(); }use std::sync::{Arc, Mutex}; const N: uint = 10; // Spawn a few threads to increment a shared variable (non-atomically), and // let the main thread know once all increments are done. // // Here we're using an Arc to share memory among tasks, and the data inside // the Arc is protected with a mutex. let data = Arc::new(Mutex::new(0)); let (tx, rx) = channel(); for _ in range(0u, 10) { let (data, tx) = (data.clone(), tx.clone()); spawn(proc() { // The shared static can only be accessed once the lock is held. // Our non-atomic increment is safe because we're the only thread // which can access the shared state when the lock is held. let mut data = data.lock(); *data += 1; if *data == N { tx.send(()); } // the lock is unlocked here when `data` goes out of scope. }); } rx.recv();
Methods
impl<T: Send> Mutex<T>
fn new(t: T) -> Mutex<T>
Creates a new mutex in an unlocked state ready for use.
fn lock(&self) -> MutexGuard<T>
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.
Panics
If another user of this mutex panicked while holding the mutex, then this call will immediately panic once the mutex is acquired.
fn try_lock(&self) -> Option<MutexGuard<T>>
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.
Panics
If another user of this mutex panicked while holding the mutex, then this call will immediately panic if the mutex would otherwise be acquired.