Wake another thread periodically with Rust

Condvar in Rust is good for waking another thread, but in this case below, I don't really need the true, I just want to wake the other thread periodically

use std::sync::{Arc, Mutex, Condvar};
use std::thread;
fn main() {
    let pair = Arc::new((Mutex::new(false), Condvar::new()));
    let pair2 = Arc::clone(&pair);
    // Inside of our lock, spawn a new thread, and then wait for it to start.
    thread::spawn(move|| {
        let (lock, cvar) = &*pair2;
        let mut started = lock.lock().unwrap();
        // We notify the condvar that the value has changed.
            *started = true;
    // Wait for the thread to start up.
    let (lock, cvar) = &*pair;
    let mut started = lock.lock().unwrap();
    loop {
        started = cvar.wait(started).unwrap();

Also, this example does not even work, I don't know why. It should wake the main thread every 20 ms.

I've used park, with success, for such things.

The caveat...

A call to park does not guarantee that the thread will remain parked forever, and callers should be prepared for this possibility.

This requirement lacks a lot of detail. Do you care to be as accurate as possible? Every wait for 20ms will only wait at least 20ms, and the additions can add up over time; if accuracy is important, then you can account for any unexpected delays and let subsequent iterations catch-up so to speak.

Or do you only care about wake-ups happening every-so-often and chose 20ms arbitrarily? Related, do you require to completely prevent any spurious additional wake-ups?

I do not care about spurious wake ups, and it should be at least 20ms.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.