Skip to content

Commit

Permalink
Rollup merge of rust-lang#42597 - mark-buer:park_timeout_example_fix,…
Browse files Browse the repository at this point in the history
… r=alexcrichton

Capture elapsed duration in Thread::park_timeout example

`beginning_park.elapsed()` might return a larger value within the loop as compared to that checked in the loop conditional.
Since `Duration` arithmetic is checked, hitting such an edge case will cause a panic.
  • Loading branch information
frewsxcv authored Jun 13, 2017
2 parents 7463cf5 + 0389d40 commit 664ab45
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,16 @@ pub fn park_timeout_ms(ms: u32) {
///
/// 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);
/// let mut timeout_remaining = timeout;
/// loop {
/// park_timeout(timeout_remaining);
/// let elapsed = beginning_park.elapsed();
/// if elapsed >= timeout {
/// break;
/// }
/// println!("restarting park_timeout after {:?}", elapsed);
/// timeout_remaining = timeout - elapsed;
/// }
/// ```
///
Expand Down

0 comments on commit 664ab45

Please sign in to comment.