Mutex¶
| Spice | |
|---|---|
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 | |
|---|---|
Initialize a fresh, unlocked mutex.
ctor¶
| Spice | |
|---|---|
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 | |
|---|---|
Destroy the underlying pthread mutex and release its storage.
Methods¶
acquire¶
| Spice | |
|---|---|
Block until the mutex is acquired by the calling thread.
release¶
| Spice | |
|---|---|
Release the mutex, allowing another waiting thread to acquire it.
tryAcquire¶
| Spice | |
|---|---|
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 | |
|---|---|
Acquire the mutex
Parameters
| Name | Type | Description |
|---|---|---|
mutex |
Mutex& |
dtor¶
| Spice | |
|---|---|
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 | |
|---|---|
Initialize a fresh condition variable.
ctor¶
| Spice | |
|---|---|
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 | |
|---|---|
Destroy the underlying pthread condition variable and release its storage.
Methods¶
wait¶
| Spice | |
|---|---|
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 | |
|---|---|
Wake up one of the threads, that are waiting on the condition variable, if any.
notifyAll¶
| Spice | |
|---|---|
Wake up all threads, that are waiting on the condition variable.
Operators¶
operator=¶
| Spice | |
|---|---|
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 | |
|---|---|
Assignment for ConditionVariable. Keeps the receiver's handle in place, for the same reasons as for Mutex.
Parameters
| Name | Type | Description |
|---|---|---|
_other |
const ConditionVariable& |