Skip to main content

task_schedule_once

Function task_schedule_once 

Source
pub fn task_schedule_once(
    delay: Duration,
    callback: Box<dyn FnOnce() + Send>,
) -> TaskHandle
Expand description

Register a one-shot task that fires once after delay elapses.

Provided for parity with the C schedule_task_1 shape. The handle can be cancelled before the deadline to suppress execution.

ยงExamples

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use dynomite::core::task::task_schedule_once;

let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
    let fired = Arc::new(AtomicBool::new(false));
    let f = fired.clone();
    let _h = task_schedule_once(Duration::from_millis(5), Box::new(move || {
        f.store(true, Ordering::Relaxed);
    }));
    tokio::time::sleep(Duration::from_millis(20)).await;
    assert!(fired.load(Ordering::Relaxed));
});