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

Add JoinHandle::is_running. #90439

Merged
merged 3 commits into from
Nov 2, 2021
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
9 changes: 9 additions & 0 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,15 @@ impl<T> JoinHandle<T> {
pub fn join(mut self) -> Result<T> {
self.0.join()
}

/// Checks if the the associated thread is still running its main function.
///
/// This might return `false` for a brief moment after the thread's main
/// function has returned, but before the thread itself has stopped running.
#[unstable(feature = "thread_is_running", issue = "90470")]
pub fn is_running(&self) -> bool {
Arc::strong_count(&self.0.packet.0) > 1
}
}

impl<T> AsInner<imp::Thread> for JoinHandle<T> {
Expand Down
36 changes: 35 additions & 1 deletion library/std/src/thread/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use super::Builder;
use crate::any::Any;
use crate::mem;
use crate::result;
use crate::sync::mpsc::{channel, Sender};
use crate::sync::{
mpsc::{channel, Sender},
Arc, Barrier,
};
use crate::thread::{self, ThreadId};
use crate::time::Duration;
use crate::time::Instant;

// !!! These tests are dangerous. If something is buggy, they will hang, !!!
// !!! instead of exiting cleanly. This might wedge the buildbots. !!!
Expand Down Expand Up @@ -46,6 +50,36 @@ fn test_run_basic() {
rx.recv().unwrap();
}

#[test]
fn test_is_running() {
let b = Arc::new(Barrier::new(2));
let t = thread::spawn({
let b = b.clone();
move || {
b.wait();
1234
}
});

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

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

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

// Joining the thread should not block for a significant time now.
let join_time = Instant::now();
assert_eq!(t.join().unwrap(), 1234);
assert!(join_time.elapsed() < Duration::from_secs(2));
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this relaible? Maybe Duration::from_secs(2 + 1.0) better?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why would three seconds be more reliable than two?

}

#[test]
fn test_join_panic() {
match thread::spawn(move || panic!()).join() {
Expand Down