# 1.0.0[−][src]Struct std::thread::JoinHandle

    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.

# Examples

Creation from `thread::spawn`:

    use std::thread;

    let join_handle: thread::JoinHandle<_> = thread::spawn(|| {
        // some work here
    });Run

Creation from `thread::Builder::spawn`:

    use std::thread;

    let builder = thread::Builder::new();

    let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
        // some work here
    }).unwrap();Run

Child being detached and outliving its parent:

    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));Run

## Methods

###

`impl<T> JoinHandle<T>`

[src]

####

`pub fn thread(&self) -> &Thread`

[src]

Extracts a handle to the underlying thread.

# Examples

    use std::thread;

    let builder = thread::Builder::new();

    let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
        // some work here
    }).unwrap();

    let thread = join_handle.thread();
    println!("thread id: {:?}", thread.id());Run

####

`pub fn join(self) -> Result<T>`

[src]

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`.

# 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 std::thread;

    let builder = thread::Builder::new();

    let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
        // some work here
    }).unwrap();
    join_handle.join().expect("Couldn't join on the associated thread");Run

## Trait Implementations

###

`impl<T> JoinHandleExt for JoinHandle<T>`

1.9.0
[src]

####

`fn as_pthread_t(&self) -> RawPthread`

[src]

This is supported on Unix only.

Extracts the raw pthread_t without taking ownership

####

`fn into_pthread_t(self) -> RawPthread`

[src]

This is supported on Unix only.

Consumes the thread, returning the raw pthread_t Read more

###

`impl<T> AsRawHandle for JoinHandle<T>`

1.9.0
[src]

####

`fn as_raw_handle(&self) -> RawHandle`

[src]

This is supported on Windows only.

Extracts the raw handle, without taking any ownership.

###

`impl<T> IntoRawHandle for JoinHandle<T>`

1.9.0
[src]

####

`fn into_raw_handle(self) -> RawHandle`

[src]

This is supported on Windows only.

Consumes this object, returning the raw underlying handle. Read more

###

`impl<T> Debug for JoinHandle<T>`

1.16.0
[src]

####

`fn fmt(&self, f: &mut Formatter) -> Result`

[src]

Formats the value using the given formatter. Read more

###

`impl<T> Send for JoinHandle<T>`

1.29.0
[src]

###

`impl<T> Sync for JoinHandle<T>`

1.29.0
[src]

## Blanket Implementations

###

`impl<T, U> TryFrom for T where T: From<U>,`

[src]

#### `type Error = !`

🔬 This is a nightly-only experimental API. (`try_from` #33417)

The type returned in the event of a conversion error.

####

`fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`

[src]

🔬 This is a nightly-only experimental API. (`try_from` #33417)

Performs the conversion.

###

`impl<T> From for T`

[src]

####

`fn from(t: T) -> T`

[src]

Performs the conversion.

###

`impl<T, U> TryInto for T where U: TryFrom<T>,`

[src]

#### `type Error = <U as TryFrom<T>>::Error`

🔬 This is a nightly-only experimental API. (`try_from` #33417)

The type returned in the event of a conversion error.

####

`fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`

[src]

🔬 This is a nightly-only experimental API. (`try_from` #33417)

Performs the conversion.

###

`impl<T, U> Into for T where U: From<T>,`

[src]

####

`fn into(self) -> U`

[src]

Performs the conversion.

###

`impl<T> Borrow for T where T: ?Sized,`

[src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'a, R: Read + ?Sized> Read for &'a mut Rimpl<'a, W: Write + ?Sized> Write for &'a mut W`

`fn borrow(&self) -> &T`

[src]

Immutably borrows from an owned value. Read more

###

`impl<T> BorrowMut for T where T: ?Sized,`

[src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'a, R: Read + ?Sized> Read for &'a mut Rimpl<'a, W: Write + ?Sized> Write for &'a mut W`

`fn borrow_mut(&mut self) -> &mut T`

[src]

Mutably borrows from an owned value. Read more

###

`impl<T> Any for T where T: 'static + ?Sized,`

[src]

####

`fn get_type_id(&self) -> TypeId`

[src]

🔬 This is a nightly-only experimental API. (`get_type_id` #27745)

this method will likely be replaced by an associated static

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)