Skip to content

Commit 4fc6f5a

Browse files
committed
Add an example to std::thread::park_timeout
1 parent ea07d52 commit 4fc6f5a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libstd/thread/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,25 @@ pub fn park_timeout_ms(ms: u32) {
478478
///
479479
/// Platforms which do not support nanosecond precision for sleeping will have
480480
/// `dur` rounded up to the nearest granularity of time they can sleep for.
481+
///
482+
/// # Example
483+
///
484+
/// Waiting for the complete expiration of the timeout:
485+
///
486+
/// ```rust,no_run
487+
/// use std::thread::park_timeout;
488+
/// use std::time::{Instant, Duration};
489+
///
490+
/// let timeout = Duration::from_secs(2);
491+
/// let beginning_park = Instant::now();
492+
/// park_timeout(timeout);
493+
///
494+
/// while beginning_park.elapsed() < timeout {
495+
/// println!("restarting park_timeout after {:?}", beginning_park.elapsed());
496+
/// let timeout = timeout - beginning_park.elapsed();
497+
/// park_timeout(timeout);
498+
/// }
499+
/// ```
481500
#[stable(feature = "park_timeout", since = "1.4.0")]
482501
pub fn park_timeout(dur: Duration) {
483502
let thread = current();

0 commit comments

Comments
 (0)