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
| Property | Type |
|---|---|
inner | CondVarInner* |
Constructors
static new() -> CondVar;
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;
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;
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;
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
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct CondVar {
inner: CondVarInner*;
}CondVar: condition variable for blocking until a predicate holds.
CondVar.inner: CondVarInner*type struct CondVarInner {
pthread_buf: [u8;
48];
}static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static CondVar::new() -> CondVarConstruct a fresh condition variable with default attributes. Panics on allocation failure or pthread init failure.
type struct CondVar {
inner: CondVarInner*;
}CondVar: condition variable for blocking until a predicate holds.
std::sync::condvarstatic CondVar::try_new() -> Result<CondVar, AllocError>type struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}CondVar::wait<T, A>(&this, mutex: &Mutex<T, A>, guard: mut &MutexGuard<T, A>) -> voidBlock 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.
type parameter Ttype parameter A&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) mutex: &Mutex<T, A>type struct Mutex<T, A = GlobalAlloc> where A: Allocator {
inner: MutexInner<T, A>*;
}Mutex<T>: mutual-exclusion lock around a value.
std::sync::mutextype parameter Ttype parameter A(parameter) guard: mut &MutexGuard<T, A>mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
type struct MutexGuard<T, A> {
inner: MutexInner<T, A>*;
}RAII guard returned by Mutex::lock. Carries a raw pointer to the inner so it can release the lock on drop. Deref via as_ptr() to read or mutate the guarded value.
CondVar::notify_one(&this) -> voidWake 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).
CondVar::notify_all(&this) -> voidWake up every thread currently waiting on this condition variable. Spurious wake-ups are possible regardless; the consumer must re-check the predicate after waking.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut guard: MutexGuard<Queue, GlobalAlloc>type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
while (condition) { ... }Repeats its body while the condition holds. The condition is checked before every iteration, so the body may run zero times.
language referencemut guard: MutexGuard<Queue, GlobalAlloc>MutexGuard::as_ptr(&this) -> T*Pointer to the guarded value. Valid for the lifetime of this guard; never null.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::sync::condvar;CondVar: condition variable for blocking until a predicate holds.
module pageCondVar::wait<T, A>(&this, mutex: &Mutex<T, A>, guard: mut &MutexGuard<T, A>) -> voidBlock 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.
CondVar::notify_one(&this) -> voidWake 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).
std::sync::condvarCondVar::notify_all(&this) -> voidWake up every thread currently waiting on this condition variable. Spurious wake-ups are possible regardless; the consumer must re-check the predicate after waking.
std::sync::condvarif (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
type trait Send {}Types that are safe to transfer between threads. An owning value of a Send type may cross a thread boundary without violating memory safety.
type trait Sync {}Types that are safe to share between threads by shared reference. T: Sync if &T: Send.
CondVar.inner: CondVarInner*static CondVar::new() -> CondVarConstruct a fresh condition variable with default attributes. Panics on allocation failure or pthread init failure.
std::sync::condvarstatic CondVar::try_new() -> Result<CondVar, AllocError>type struct MutexInner<T, A> {
pthread_buf: [u8;
40];
value: T;
alloc: A;
}implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference