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

Rename JoinHandle::is_running to is_finished. #94549

Merged
merged 3 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
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
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