use std::{
sync::{Condvar, Mutex},
time::Duration,
};
/// A countdown latch is like an [`std::sync::Barrier`] that lets M threads /// wait on N operations to complete. Unlike a barrier, a countdown latch is /// acyclic: once the latch has counted down to zero, it can't be reset. pubstruct CountDownLatch {
count: Mutex<usize>,
cvar: Condvar,
}
impl CountDownLatch { /// Creates a new latch that waits for N operations to complete. pubconstfn new(count: usize) -> Self { Self {
count: Mutex::new(count),
cvar: Condvar::new(),
}
}
/// Decrements the count of the latch, waking up any waiting threads /// if the count reaches zero. Returns without effect if the count is /// already zero. pubfn count_down(&self) { letmut count = self.count.lock().unwrap(); if *count > 0 {
*count -= 1; if *count == 0 { self.cvar.notify_all();
}
}
}
/// Waits for either the count of the latch to reach zero, or the `timeout` /// to elapse; whichever happens first. Returns `true` if the count is /// already zero, or reaches zero within the `timeout`. pubfn wait_timeout(&self, timeout: Duration) -> bool { letmut count = self.count.lock().unwrap(); while *count > 0 { let (guard, result) = self.cvar.wait_timeout(count, timeout).unwrap(); if result.timed_out() { returnfalse;
}
count = guard;
} true
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.