Open
Description
The description of std::thread::sleep() says:
Puts the current thread to sleep for at least the specified amount of time.
The thread may sleep longer than the duration specified due to scheduling specifics or platform-dependent functionality. It will never sleep less.
and it gives the example code:
use std::{thread, time};
let ten_millis = time::Duration::from_millis(10);
let now = time::Instant::now();
thread::sleep(ten_millis);
assert!(now.elapsed() >= ten_millis);
But this assertion often fails when I run this code.
If the description is correct and it shouldn't fail, it happens on Windows 7 32 bit, stable-i686-pc-windows-gnu
- rustc 1.32.0 (9fda7c223 2019-01-16)
EDIT:
Though, the expected behaviour can be achieved with this wrapper function:
use std::time::{Duration, Instant};
use std::thread::sleep;
fn sleep_noless(wanted_sleep: Duration) {
let now = Instant::now();
let mut slept_total = now.elapsed();
while slept_total < wanted_sleep {
sleep(wanted_sleep - slept_total);
slept_total = now.elapsed();
}
}