pub struct Condvar { /* private fields */ }
Expand description
a primitive to signal and wait on a condition
Implementations§
source§impl Condvar
impl Condvar
sourcepub fn wait<'a, T>(&self, mutex_guard: MutexGuard<'a, T>) -> MutexGuard<'a, T>
pub fn wait<'a, T>(&self, mutex_guard: MutexGuard<'a, T>) -> MutexGuard<'a, T>
Wait on the condition variable until notified.
This function will atomically unlock the mutex, and then wait for a notification.
Examples
use std::thread;
use std::time::Duration;
use std::sync::Arc;
use lib_wc::sync::{Condvar, Mutex};
let mutex = Arc::new(Mutex::new(0));
let condvar = Condvar::new();
let mutex = Mutex::new(0);
let condvar = Condvar::new();
let mut wakeups = 0;
thread::scope(|s| {
s.spawn(|| {
thread::sleep(Duration::from_nanos(10));
*mutex.lock() = 123;
condvar.notify_one();
});
let mut m = mutex.lock();
while *m < 100 {
m = condvar.wait(m);
wakeups += 1;
}
assert_eq!(*m, 123);
});
// Check that the main thread actually did wait (not busy-loop),
// while still allowing for a few spurious wake ups.
assert!(wakeups < 10);