dd: Simplify loop of progress Alarm thread

This commit is contained in:
Thomas Hurst 2023-03-01 13:56:18 +00:00
parent cf7b90bbe7
commit 52c93a4d10

View file

@ -80,20 +80,18 @@ pub struct Alarm {
} }
impl Alarm { impl Alarm {
pub fn with_interval(interval: Duration) -> Alarm { pub fn with_interval(interval: Duration) -> Self {
let trigger = Arc::new(AtomicBool::default()); let trigger = Arc::new(AtomicBool::default());
let weak_trigger = Arc::downgrade(&trigger); let weak_trigger = Arc::downgrade(&trigger);
std::thread::spawn(move || loop { std::thread::spawn(move || {
while let Some(trigger) = weak_trigger.upgrade() {
sleep(interval); sleep(interval);
if let Some(trigger) = weak_trigger.upgrade() {
trigger.store(true, Relaxed); trigger.store(true, Relaxed);
} else {
break;
} }
}); });
Alarm { interval, trigger } Self { interval, trigger }
} }
pub fn is_triggered(&self) -> bool { pub fn is_triggered(&self) -> bool {