Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture elapsed duration in Thread::park_timeout example #42597

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should compare against timeout_remaining, right?

Similarly, below the subtraction should be against timeout_remaining instead of timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I think it is right as-is.

In the loop, elapsed is the total duration since creation of beginning_park (created outside of the loop). It is a rough measure of the total duration we've parked for thus far.
timeout is the total desired park duration.
Thus, comparing the two seems okay (we're comparing two "totals").
If we need to park some more, we calculate the difference between the total desired park duration and the total elapsed park duration, and use that value in yet-another-park.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right yes sorry got mixed up!

/// break;
/// }
/// println!("restarting park_timeout after {:?}", elapsed);
/// timeout_remaining = timeout - elapsed;
/// }
/// ```
///
Expand Down