Skip to main content

sleep_precise

Function sleep_precise 

Source
pub fn sleep_precise(duration: Duration) -> SleepPrecise ⓘ
Expand description

Waits until duration has passed, to the microsecond.

The wait is at least duration, rounded up to the microsecond, and ends as soon after that as the system’s timer wakes the shard; a busy shard may let it run later by the time its current task takes to yield. Use it where the millisecond of sleep is too coarse: a pacer, a retry spaced in microseconds, a deadline a protocol counts in microseconds. Each precise deadline that is the shard’s earliest costs the shard a timer of its own in the kernel (one system call to arm it on epoll), so a coarse sleep stays the cheaper choice where a millisecond is fine.

use std::time::Duration;
use nuclei::time::{self, Instant};

time::pause();
let start = Instant::now();
time::sleep_precise(Duration::from_micros(250)).await;
// A paused clock moves to the deadline itself: not a millisecond later.
assert_eq!(start.elapsed(), Duration::from_micros(250));