Stdlibsync
mutex
import std::sync::mutex; · source
Mutex<T, A>
type struct Mutex<T, A = GlobalAlloc> {
inner: MutexInner<T, A>*;
static new(value: T) -> Mutex<T, GlobalAlloc>;
static new_in(value: T, alloc: A) -> Mutex<T, A>;
static try_new(value: T) -> Result<Mutex<T, GlobalAlloc>, AllocError>;
static try_new_in(value: T, alloc: A) -> Result<Mutex<T, A>, AllocError>;
lock(&this) -> MutexGuard<T, A>;
try_lock(&this) -> Option<MutexGuard<T, A>>;
raw_pthread_buf(&this) -> u8*;
}
At most one thread at a time can read or mutate the guarded value.
import std::sync::mutex;
mut m: Mutex<Counter> = Mutex<Counter>::new(counter);
mut g: MutexGuard<Counter, GlobalAlloc> = m.lock();
g.as_ptr().hits = g.as_ptr().hits + 1;
MutexGuard<T, A>
type struct MutexGuard<T, A> {
inner: MutexInner<T, A>*;
as_ptr(&this) -> T*;
}
lock() blocks; try_lock() returns None if the lock is held. The guard dereferences through as_ptr() -> T*, valid for the guard's lifetime, and releases the lock when its drop runs. raw_pthread_buf exposes the underlying primitive for CondVar, which needs to hand it to pthread_cond_wait.
Trait implementations
implement<T, A> trait Drop for struct Mutex<T, A>
where T: Drop, A: Allocator
implement<T, A> trait Drop for struct MutexGuard<T, A>