diff options
| author | Corey Farwell <coreyf@rwell.org> | 2017-06-13 17:15:02 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-06-13 17:15:02 -0400 |
| commit | 664ab45796bfd23cb04bcf49eb8ec2b9babc7f39 (patch) | |
| tree | d0d6316cf431080679a7bc68ddf756b217b35ee5 /src/libstd/thread | |
| parent | 7463cf5faf90c3f9c99077267520d53d73393591 (diff) | |
| parent | 0389d40ce0a891bc7d37ca15c12e23df79d71524 (diff) | |
| download | rust-664ab45796bfd23cb04bcf49eb8ec2b9babc7f39.tar.gz rust-664ab45796bfd23cb04bcf49eb8ec2b9babc7f39.zip | |
Rollup merge of #42597 - mark-buer:park_timeout_example_fix, 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.
Diffstat (limited to 'src/libstd/thread')
| -rw-r--r-- | src/libstd/thread/mod.rs | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 64c31c2a681..dda11e50380 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -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; /// } /// ``` /// |
