Skip to content
CryoCryo home
Stdlibsync

condvar

import std::sync::condvar; · source

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.

Trait implementations

implement trait Drop for struct CondVar