Skip to content
CryoCryo home
Stdlibsync

mutex

import std::sync::mutex; · source

Types

Mutex<T, A>

type struct Mutex<T, A = GlobalAlloc> where A: Allocator {
    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;

Properties

PropertyType
innerMutexInner<T, A>*

Constructors

static new(value: T) -> Mutex<T, GlobalAlloc>;

Construct a Mutex<T> initialized to hold value, using GlobalAlloc for the inner allocation. Panics on allocation failure; use try_new to handle the error.

static new_in(value: T, alloc: A) -> Mutex<T, A>;
static try_new(value: T) -> Result<Mutex<T, GlobalAlloc>, AllocError>;

Fallible constructor. Initializes the pthread mutex with default attributes (PTHREAD_MUTEX_NORMAL). Returns Err(AllocError) either when the inner buffer cannot be allocated or when pthread_mutex_init itself fails (ENOMEM or EAGAIN under resource pressure); in the latter case the just-allocated buffer is released before returning so the caller never leaks on the error path.

static try_new_in(value: T, alloc: A) -> Result<Mutex<T, A>, AllocError>;

Methods

lock(&this) -> MutexGuard<T, A>;

Acquire the lock, blocking the current thread until it is available. Returns a guard whose drop() will release the lock. Panics only on a programming error reported by pthread_mutex_lock (EINVAL from a recursively-locked non-recursive mutex or a destroyed mutex).

try_lock(&this) -> Option<MutexGuard<T, A>>;

Non-blocking lock attempt. Returns Some(guard) when the lock was acquired; None when the lock was already held by another thread.

raw_pthread_buf(&this) -> u8*;

Raw pointer to the inner pthread buffer. Used by CondVar to pair the mutex with a condition variable in pthread_cond_wait; not a stable public API.

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.

Properties
PropertyType
innerMutexInner<T, A>*
Methods
as_ptr(&this) -> T*;

Pointer to the guarded value. Valid for the lifetime of this guard; never null.

Trait implementations

Mutex
implement<T, A> trait Drop for struct Mutex<T, A> where A: Allocator
MutexGuard
implement<T, A> trait Drop for struct MutexGuard<T, A>