Skip to content
CryoCryo home
Stdlibsync

condvar

import std::sync::condvar; · source

Types

CondVar

type struct CondVar {
    inner: CondVarInner*;

    static new() -> CondVar;
    static try_new() -> Result<CondVar, AllocError>;
    wait<T, A>(&this, mutex: &Mutex<T, A>, guard: mut &MutexGuard<T, A>) -> void;
    notify_one(&this) -> void;
    notify_all(&this) -> void;
}

Atomically release a Mutex and sleep until another thread signals — the standard producer/consumer primitive.

mut guard: MutexGuard<Queue, GlobalAlloc> = shared.lock();
while (guard.as_ptr().is_empty()) {
    cond.wait(&shared, &mut guard);
}
// act on the queue

wait(mutex, guard) releases the lock, sleeps, and reacquires before returning. The guard stays "locked" from your perspective; pthread does the unlock and relock internally, and the guard's address still refers to the same inner.

notify_one() wakes one waiter, notify_all() wakes all. If nothing is waiting, a signal is lost — standard pthread semantics, which is why the consumer must check the predicate before sleeping. Spurious wake-ups are possible regardless, so re-check the predicate after waking; that is why the example loops rather than using an if.

Cryo has no borrow checker, so it is on you not to touch the guarded value while inside wait() — doing so reads through the guard while another thread holds the lock.

CondVar is Send + Sync unconditionally.

Properties

PropertyType
innerCondVarInner*

Constructors

static new() -> CondVar;

Construct a fresh condition variable with default attributes. Panics on allocation failure or pthread init failure.

static try_new() -> Result<CondVar, AllocError>;

Methods

wait<T, A>(&this, mutex: &Mutex<T, A>, guard: mut &MutexGuard<T, A>) -> void;

Block the current thread until another thread signals this condition variable. Atomically releases mutex, waits, and reacquires mutex before returning. guard is not invalidated - its address still refers to the same MutexInner - but inspect the guarded value only after wait returns.

notify_one(&this) -> void;

Wake up one thread waiting on this condition variable. If no thread is currently waiting, the signal is lost (this is the standard pthread semantics; the producer should arrange for the consumer to check the predicate before sleeping).

notify_all(&this) -> void;

Wake up every thread currently waiting on this condition variable. Spurious wake-ups are possible regardless; the consumer must re-check the predicate after waking.

Trait implementations

implement trait Drop for struct CondVar