未验证 提交 ee3a2c7a 编写于 作者: M Matthias Krüger 提交者: GitHub

Rollup merge of #94549 - m-ou-se:thread-is-finished, r=yaahc

Rename JoinHandle::is_running to is_finished.

This is renaming `is_running` to `is_finished` as discussed on the tracking issue here: https://github.com/rust-lang/rust/issues/90470#issuecomment-1050188499

Taking some of the docs suggestions from https://github.com/rust-lang/rust/pull/94033
...@@ -1443,13 +1443,18 @@ pub fn join(self) -> Result<T> { ...@@ -1443,13 +1443,18 @@ pub fn join(self) -> Result<T> {
self.0.join() 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. /// 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")] #[unstable(feature = "thread_is_running", issue = "90470")]
pub fn is_running(&self) -> bool { pub fn is_finished(&self) -> bool {
Arc::strong_count(&self.0.packet) > 1 Arc::strong_count(&self.0.packet) == 1
} }
} }
......
...@@ -240,7 +240,6 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> { ...@@ -240,7 +240,6 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
/// ///
/// ``` /// ```
/// #![feature(scoped_threads)] /// #![feature(scoped_threads)]
/// #![feature(thread_is_running)]
/// ///
/// use std::thread; /// use std::thread;
/// ///
...@@ -274,7 +273,6 @@ pub fn thread(&self) -> &Thread { ...@@ -274,7 +273,6 @@ pub fn thread(&self) -> &Thread {
/// ///
/// ``` /// ```
/// #![feature(scoped_threads)] /// #![feature(scoped_threads)]
/// #![feature(thread_is_running)]
/// ///
/// use std::thread; /// use std::thread;
/// ///
...@@ -289,13 +287,18 @@ pub fn join(self) -> Result<T> { ...@@ -289,13 +287,18 @@ pub fn join(self) -> Result<T> {
self.0.join() 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. /// 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")] #[unstable(feature = "thread_is_running", issue = "90470")]
pub fn is_running(&self) -> bool { pub fn is_finished(&self) -> bool {
Arc::strong_count(&self.0.packet) > 1 Arc::strong_count(&self.0.packet) == 1
} }
} }
......
...@@ -52,7 +52,7 @@ fn test_run_basic() { ...@@ -52,7 +52,7 @@ fn test_run_basic() {
} }
#[test] #[test]
fn test_is_running() { fn test_is_finished() {
let b = Arc::new(Barrier::new(2)); let b = Arc::new(Barrier::new(2));
let t = thread::spawn({ let t = thread::spawn({
let b = b.clone(); let b = b.clone();
...@@ -63,14 +63,14 @@ fn test_is_running() { ...@@ -63,14 +63,14 @@ fn test_is_running() {
}); });
// Thread is definitely running here, since it's still waiting for the barrier. // 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. // Unblock the barrier.
b.wait(); 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(); let start = Instant::now();
while t.is_running() { while !t.is_finished() {
assert!(start.elapsed() < Duration::from_secs(2)); assert!(start.elapsed() < Duration::from_secs(2));
thread::sleep(Duration::from_millis(15)); thread::sleep(Duration::from_millis(15));
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册