Struct JoinHandle
pub struct JoinHandle<T>(pub(in ::thread) JoinInner<'static, T>);
An owned permission to join on a thread (block on its termination).
A JoinHandle detaches the associated thread when it is dropped, which
means that there is no longer any handle to the thread and no way to join
on it.
Due to platform restrictions, it is not possible to Clone this
handle: the ability to join a thread is a uniquely-owned permission.
This struct is created by the thread::spawn function and the
thread::Builder::spawn method.
Examples
Creation from thread::spawn:
use thread;
let join_handle: JoinHandle = spawn;
Creation from thread::Builder::spawn:
use thread;
let builder = new;
let join_handle: JoinHandle = builder.spawn.unwrap;
A thread being detached and outliving the thread that spawned it:
use std::thread;
use std::time::Duration;
let original_thread = thread::spawn(|| {
let _detached_thread = thread::spawn(|| {
// Here we sleep to make sure that the first thread returns before.
thread::sleep(Duration::from_millis(10));
// This will be called, even though the JoinHandle is dropped.
println!("♫ Still alive ♫");
});
});
original_thread.join().expect("The thread being joined has panicked");
println!("Original thread is joined.");
// We make sure that the new thread has time to run, before the main
// thread returns.
thread::sleep(Duration::from_millis(1000));
Fields
0: JoinInner<'static, T>
Implementations
impl<T> JoinHandle<T>
fn thread(&self) -> &ThreadExtracts a handle to the underlying thread.
Examples
use thread; let builder = new; let join_handle: JoinHandle = builder.spawn.unwrap; let thread = join_handle.thread; println!;fn join(self) -> Result<T>Waits for the associated thread to finish.
This function will return immediately if the associated thread has already finished. Otherwise, it fully waits for the thread to finish, including all destructors for thread-local variables that might be running after the main function of the thread.
In terms of atomic memory orderings, the completion of the associated thread synchronizes with this function returning. In other words, all operations performed by that thread happen before all operations that happen after
joinreturns.If the associated thread panics,
Erris returned with the parameter given to [panic!] (though see the Notes below).Panics
This function may panic on some platforms if a thread attempts to join itself or otherwise may create a deadlock with joining threads.
Examples
use thread; let builder = new; let join_handle: JoinHandle = builder.spawn.unwrap; join_handle.join.expect;Notes
If a "foreign" unwinding operation (e.g. an exception thrown from C++ code, or a
panic!in Rust code compiled or linked with a different runtime) unwinds all the way to the thread root, the process may be aborted; see the Notes onthread::spawn. If the process is not aborted, this function will return aResult::Errcontaining an opaque type.fn is_finished(&self) -> boolChecks if the associated thread has finished running its main function.
is_finishedsupports implementing a non-blocking join operation, by checkingis_finished, and callingjoinif it returnstrue. This function does not block. To block while waiting on the thread to finish, use [join][Self::join].This might return
truefor a brief moment after the thread's main function has returned, but before the thread itself has stopped running. However, once this returnstrue, [join][Self::join] can be expected to return quickly, without blocking for any significant amount of time.
Trait Implementations
impl<T> AsHandle for JoinHandle<T>
fn as_handle(&self) -> BorrowedHandle<'_>
impl<T> AsInner<Thread> for JoinHandle<T>
fn as_inner(&self) -> &Thread
impl<T> AsRawHandle for JoinHandle<T>
fn as_raw_handle(&self) -> RawHandle
impl<T> Debug for JoinHandle<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T> IntoInner<Thread> for JoinHandle<T>
fn into_inner(self) -> Thread
impl<T> IntoRawHandle for JoinHandle<T>
fn into_raw_handle(self) -> RawHandle
impl<T> JoinHandleExt for JoinHandle<T>
fn as_pthread_t(&self) -> RawPthreadfn into_pthread_t(self) -> RawPthread
impl<T> Send for JoinHandle<T>
impl<T> Sync for JoinHandle<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for JoinHandle<T>
impl<T> !UnwindSafe for JoinHandle<T>
impl<T> Freeze for JoinHandle<T>
where
JoinInner<'static, T>: Freeze,
impl<T> Unpin for JoinHandle<T>
where
JoinInner<'static, T>: Unpin,
impl<T> UnsafeUnpin for JoinHandle<T>
where
JoinInner<'static, T>: UnsafeUnpin,
Blanket Implementations
impl<T> Any for JoinHandle<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for JoinHandle<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for JoinHandle<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for JoinHandle<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> SizeHint for JoinHandle<T>
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for JoinHandle<T>
impl<T, U> Into<U> for JoinHandle<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for JoinHandle<T>
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for JoinHandle<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>