W3cubDocs

/Rust

Function std::thread::park_timeout

pub fn park_timeout(dur: Duration)

Blocks unless or until the current thread's token is made available or the specified duration has been reached (may wake spuriously).

The semantics of this function are equivalent to park() except that the thread will be blocked for roughly no longer than dur. This method should not be used for precise timing due to anomalies such as preemption or platform differences that may not cause the maximum amount of time waited to be precisely dur long.

See the module doc for more detail.

Platform behavior

Platforms which do not support nanosecond precision for sleeping will have dur rounded up to the nearest granularity of time they can sleep for.

Example

Waiting for the complete expiration of the timeout:

use std::thread::park_timeout;
use std::time::{Instant, Duration};

let timeout = Duration::from_secs(2);
let beginning_park = Instant::now();
park_timeout(timeout);

while beginning_park.elapsed() < timeout {
    println!("restarting park_timeout after {:?}", beginning_park.elapsed());
    let timeout = timeout - beginning_park.elapsed();
    park_timeout(timeout);
}

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/thread/fn.park_timeout.html