pub struct JoinHandle<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 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.
Creation from thread::spawn
:
use std::thread;
let join_handle: thread::JoinHandle<_> = thread::spawn(|| {
});
Creation from thread::Builder::spawn
:
use std::thread;
let builder = thread::Builder::new();
let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
}).unwrap();
Child being detached and outliving its parent:
use std::thread;
use std::time::Duration;
let original_thread = thread::spawn(|| {
let _detached_thread = thread::spawn(|| {
thread::sleep(Duration::from_millis(10));
println!("♫ Still alive ♫");
});
});
original_thread.join().expect("The thread being joined has panicked");
println!("Original thread is joined.");
thread::sleep(Duration::from_millis(1000));
Extracts a handle to the underlying thread.
use std::thread;
let builder = thread::Builder::new();
let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
}).unwrap();
let thread = join_handle.thread();
println!("thread id: {:?}", thread.id());
Waits for the associated thread to finish.
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 are ordered before all
operations that happen after join
returns.
If the child thread panics, Err
is returned with the parameter given
to panic
.
This function may panic on some platforms if a thread attempts to join
itself or otherwise may create a deadlock with joining threads.
use std::thread;
let builder = thread::Builder::new();
let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
}).unwrap();
join_handle.join().expect("Couldn't join on the associated thread");
Extracts the raw pthread_t without taking ownership
Consumes the thread, returning the raw pthread_t Read more
Formats the value using the given formatter. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more