Struct bitflags::__core::sync::Condvar [] [src]

pub struct Condvar {
    // some fields omitted
}
1.0.0
[]

A Condition Variable

Condition variables represent the ability to block a thread such that it consumes no CPU time while waiting for an event to occur. Condition variables are typically associated with a boolean predicate (a condition) and a mutex. The predicate is always verified inside of the mutex before determining that thread must block.

Functions in this module will block the current thread of execution and are bindings to system-provided condition variables where possible. Note that this module places one additional restriction over the system condition variables: each condvar can be used with precisely one mutex at runtime. Any attempt to use multiple mutexes on the same condition variable will result in a runtime panic. If this is not desired, then the unsafe primitives in sys do not have this restriction but may result in undefined behavior.

Examples

use std::sync::{Arc, Mutex, Condvar};
use std::thread;

let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();

// Inside of our lock, spawn a new thread, and then wait for it to start
thread::spawn(move|| {
    let &(ref lock, ref cvar) = &*pair2;
    let mut started = lock.lock().unwrap();
    *started = true;
    cvar.notify_one();
});

// wait for the thread to start up
let &(ref lock, ref cvar) = &*pair;
let mut started = lock.lock().unwrap();
while !*started {
    started = cvar.wait(started).unwrap();
}