Skip to content
CryoCryo home
Stdlibsync

condition_variable

import std::sync::condition_variable; · source

Types

ConditionVariable

type struct ConditionVariable {
    private inner: mut ConditionVariableInner*;
}

Atomically release a Mutex and sleep until another thread signals — the standard producer/consumer primitive. A pthread_cond_t on POSIX; on Windows a CONDITION_VARIABLE waited on with the mutex's SRW lock. Use one ConditionVariable with only one Mutex.

import std::alloc::allocator::{ GlobalAllocator };
import std::sync::mutex::{ Mutex, MutexGuard };
import std::sync::condition_variable::{ ConditionVariable };

// `queued` and `cond` are shared with the producer, which raises the count and signals
function wait_for_work(queued: &Mutex<i64>, cond: &ConditionVariable) -> void {
    mut guard: MutexGuard<i64, GlobalAllocator> = queued.lock();
    while (*guard.as_pointer() == 0) {
        cond.wait(queued, &guard);
    }
    // act on the queue
}

wait(mutex, guard) releases the lock, sleeps, and reacquires before returning. The guard stays "locked" from your perspective; the platform does the unlock and relock internally, and the guard still refers to the same lock. wait_timeout(mutex, guard, duration) waits no longer than duration and answers true when the time ran out, false when the thread was woken.

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.

new() panics if the condition variable cannot be made; try_new() returns the AllocationError instead. ConditionVariable is Send + Sync unconditionally.

Constructors

static new() -> ConditionVariable;

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

static try_new() -> Result<ConditionVariable, AllocationError>;

Construct a condition variable, or Err when it cannot be made: an OutOfMemory AllocationError both when its storage cannot be allocated and when pthread_cond_init fails (unix; the Windows initialisation cannot fail).

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.

wait_timeout<T, A>(&this, mutex: &Mutex<T, A>, guard: mut &MutexGuard<T, A>, duration: Duration) -> boolean;

Wait as wait does, but for no longer than duration. Answers true when the wait ended because duration passed - never before the whole of it has, on the monotonic clock - and false when the thread was woken. A wake can be spurious, as for wait, so check the condition waited for either way.

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 ConditionVariable