std-method

Condvar::wait_timeout_while: bounded waiting

Combine a predicate with an approximate timeout. Recheck the returned state even when the timeout result indicates expiry. Scheduling and reacquisition can delay the return beyond the requested interval. A predicate already satisfied needs no sleep. Expected stdout (not executed): true Common mistake: Assuming the timeout is an exact upper bound on wall-clock return.

Formal Syntax & Structure

rust syntaxrust
cv.wait_timeout_while(guard, duration, predicate)

Working Example

Example Snippetrust
fn main() {    use std::{sync::{Mutex, Condvar}, time::Duration};    let m = Mutex::new(true);    let cv = Condvar::new();    let (g, _) = cv.wait_timeout_while(m.lock().unwrap(), Duration::from_secs(1), |r| !*r).unwrap();    println!("{}", *g);}
Rust Reference