Skip to content

Commit

Permalink
Rollup merge of rust-lang#94549 - m-ou-se:thread-is-finished, r=yaahc
Browse files Browse the repository at this point in the history
Rename JoinHandle::is_running to is_finished.

This is renaming `is_running` to `is_finished` as discussed on the tracking issue here: rust-lang#90470 (comment)

Taking some of the docs suggestions from rust-lang#94033
  • Loading branch information
matthiaskrgr authored Mar 4, 2022
2 parents 5115bdc + af86b55 commit ee3a2c7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
13 changes: 9 additions & 4 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,13 +1443,18 @@ impl<T> JoinHandle<T> {
self.0.join()
}

/// Checks if the associated thread is still running its main function.
/// Checks if the associated thread has finished running its main function.
///
/// This might return `false` for a brief moment after the thread's main
/// This might return `true` for a brief moment after the thread's main
/// function has returned, but before the thread itself has stopped running.
/// However, once this returns `true`, [`join`][Self::join] can be expected
/// to return quickly, without blocking for any significant amount of time.
///
/// This function does not block. To block while waiting on the thread to finish,
/// use [`join`][Self::join].
#[unstable(feature = "thread_is_running", issue = "90470")]
pub fn is_running(&self) -> bool {
Arc::strong_count(&self.0.packet) > 1
pub fn is_finished(&self) -> bool {
Arc::strong_count(&self.0.packet) == 1
}
}

Expand Down
15 changes: 9 additions & 6 deletions library/std/src/thread/scoped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
///
/// ```
/// #![feature(scoped_threads)]
/// #![feature(thread_is_running)]
///
/// use std::thread;
///
Expand Down Expand Up @@ -274,7 +273,6 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
///
/// ```
/// #![feature(scoped_threads)]
/// #![feature(thread_is_running)]
///
/// use std::thread;
///
Expand All @@ -289,13 +287,18 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
self.0.join()
}

/// Checks if the associated thread is still running its main function.
/// Checks if the associated thread has finished running its main function.
///
/// This might return `false` for a brief moment after the thread's main
/// This might return `true` for a brief moment after the thread's main
/// function has returned, but before the thread itself has stopped running.
/// However, once this returns `true`, [`join`][Self::join] can be expected
/// to return quickly, without blocking for any significant amount of time.
///
/// This function does not block. To block while waiting on the thread to finish,
/// use [`join`][Self::join].
#[unstable(feature = "thread_is_running", issue = "90470")]
pub fn is_running(&self) -> bool {
Arc::strong_count(&self.0.packet) > 1
pub fn is_finished(&self) -> bool {
Arc::strong_count(&self.0.packet) == 1
}
}

Expand Down
8 changes: 4 additions & 4 deletions library/std/src/thread/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn test_run_basic() {
}

#[test]
fn test_is_running() {
fn test_is_finished() {
let b = Arc::new(Barrier::new(2));
let t = thread::spawn({
let b = b.clone();
Expand All @@ -63,14 +63,14 @@ fn test_is_running() {
});

// Thread is definitely running here, since it's still waiting for the barrier.
assert_eq!(t.is_running(), true);
assert_eq!(t.is_finished(), false);

// Unblock the barrier.
b.wait();

// Now check that t.is_running() becomes false within a reasonable time.
// Now check that t.is_finished() becomes true within a reasonable time.
let start = Instant::now();
while t.is_running() {
while !t.is_finished() {
assert!(start.elapsed() < Duration::from_secs(2));
thread::sleep(Duration::from_millis(15));
}
Expand Down

0 comments on commit ee3a2c7

Please sign in to comment.