Skip to content

Mutex

Spice
import "std/os/mutex";

Mutex struct

Mutex for reserving a resource for exclusive access between threads.

Backed by a POSIX pthread_mutex_t. Calls to acquire block until the mutex is available; release wakes up another waiting thread, if any. A Mutex owns the underlying pthread handle and is not safe to share by shallow copy — use a reference (Mutex&) when passing across functions.

Constructors

ctor

Spice
public p Mutex.ctor()

Initialize a fresh, unlocked mutex.

ctor

Spice
public p Mutex.ctor(const Mutex& _other)

Copy-construct a fresh, unlocked mutex. The original mutex is not aliased, since pthread_mutex_t handles cannot be safely shared via shallow copy.

Parameters

Name Type Description
_other const Mutex&

dtor

Spice
public p Mutex.dtor()

Destroy the underlying pthread mutex and release its storage.

Methods

acquire

Spice
public p Mutex.acquire()

Block until the mutex is acquired by the calling thread.

release

Spice
public p Mutex.release()

Release the mutex, allowing another waiting thread to acquire it.

tryAcquire

Spice
public f<bool> Mutex.tryAcquire()

Try to acquire the mutex without blocking.

Returns: bool — true if the mutex was acquired, false if another thread holds it

LockGuard struct

LockGuard is a RAII wrapper for Mutex: acquires on construction and releases on destruction, ensuring the mutex is freed even when the protected scope exits via an early return or a panic.

Constructors

ctor

Spice
public p LockGuard.ctor(Mutex& mutex)

Acquire the mutex

Parameters

Name Type Description
mutex Mutex&

dtor

Spice
public p LockGuard.dtor()

Release the mutex

ConditionVariable struct

Condition variable, which lets threads sleep until another thread signals, that a condition might have changed.

Backed by a POSIX pthread_cond_t. A condition variable is always used together with a Mutex, that guards the condition: the waiting thread checks the condition while holding the mutex and calls wait, which releases the mutex while sleeping and re-acquires it before returning. Since a thread can wake up spuriously, the condition has to be re-checked in a loop after every wait. Like a Mutex, a ConditionVariable is not safe to share by shallow copy.

Constructors

ctor

Spice
public p ConditionVariable.ctor()

Initialize a fresh condition variable.

ctor

Spice
public p ConditionVariable.ctor(const ConditionVariable& _other)

Copy-construct a fresh condition variable. The original is not aliased, since pthread_cond_t handles cannot be safely shared via shallow copy.

Parameters

Name Type Description
_other const ConditionVariable&

dtor

Spice
public p ConditionVariable.dtor()

Destroy the underlying pthread condition variable and release its storage.

Methods

wait

Spice
public p ConditionVariable.wait(Mutex& mutex)

Release the given mutex and block until the condition variable is notified, then re-acquire the mutex. The calling thread must hold the mutex.

Parameters

Name Type Description
mutex Mutex& Mutex, that guards the condition and is held by the calling thread

notifyOne

Spice
public p ConditionVariable.notifyOne()

Wake up one of the threads, that are waiting on the condition variable, if any.

notifyAll

Spice
public p ConditionVariable.notifyAll()

Wake up all threads, that are waiting on the condition variable.

Operators

operator=

Spice
public p operator=(Mutex& this, const Mutex& _other)

Assignment for Mutex. Spice falls back to the copy constructor for plain struct assignment, which would unconditionally allocate a new pthread_mutex_t and leak the receiver's existing handle. A Mutex owns a unique resource, so the only meaningful "copy" is a fresh independent mutex — and the receiver already is one. We therefore keep the receiver's handle in place and only lazily initialize if it was previously destroyed or never set up.

Parameters

Name Type Description
_other const Mutex&

operator=

Spice
public p operator=(ConditionVariable& this, const ConditionVariable& _other)

Assignment for ConditionVariable. Keeps the receiver's handle in place, for the same reasons as for Mutex.

Parameters

Name Type Description
_other const ConditionVariable&