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

    pub struct Builder { /* fields omitted */ }

Thread factory, which can be used in order to configure the properties of a new thread.

Methods can be chained on it in order to configure it.

The two configurations available are:

- `name`: specifies an associated name for the thread
- `stack_size`: specifies the desired stack size for the thread

The `spawn` method will take ownership of the builder and create an `io::Result` to the thread handle with the given configuration.

The `thread::spawn` free function uses a `Builder` with default configuration and `unwrap`s its return value.

You may want to use `spawn` instead of `thread::spawn`, when you want to recover from a failure to launch a thread, indeed the free function will panic where the `Builder` method will return a `io::Result`.

# Examples

    use std::thread;

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

    let handler = builder.spawn(|| {
        // thread code
    }).unwrap();

    handler.join().unwrap();Run

## Methods

### `impl Builder`[src]

#### `pub fn new() -> Builder`[src]

Generates the base configuration for spawning a thread, from which configuration methods can be chained.

# Examples

    use std::thread;

    let builder = thread::Builder::new()
                                  .name("foo".into())
                                  .stack_size(10);

    let handler = builder.spawn(|| {
        // thread code
    }).unwrap();

    handler.join().unwrap();Run

#### `pub fn name(self, name: String) -> Builder`[src]

Names the thread-to-be. Currently the name is used for identification only in panic messages.

The name must not contain null bytes (`\0`).

For more information about named threads, see this module-level documentation.

# Examples

    use std::thread;

    let builder = thread::Builder::new()
        .name("foo".into());

    let handler = builder.spawn(|| {
        assert_eq!(thread::current().name(), Some("foo"))
    }).unwrap();

    handler.join().unwrap();Run

#### `pub fn stack_size(self, size: usize) -> Builder`[src]

Sets the size of the stack (in bytes) for the new thread.

The actual stack size may be greater than this value if the platform specifies a minimal stack size.

For more information about the stack size for threads, see this module-level documentation.

# Examples

    use std::thread;

    let builder = thread::Builder::new().stack_size(32 * 1024);Run

#### `pub fn spawn<F, T>(self, f: F) -> Result<JoinHandle<T>> where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,` [src]

Spawns a new thread by taking ownership of the `Builder`, and returns an `io::Result` to its `JoinHandle`.

The spawned thread may outlive the caller (unless the caller thread is the main thread; the whole process is terminated when the main thread finishes). The join handle can be used to block on termination of the child thread, including recovering its panics.

For a more complete documentation see `thread::spawn`.

# Errors

Unlike the `spawn` free function, this method yields an `io::Result` to capture any failure to create the thread at the OS level.

# Panics

Panics if a thread name was set and it contained null bytes.

# Examples

    use std::thread;

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

    let handler = builder.spawn(|| {
        // thread code
    }).unwrap();

    handler.join().unwrap();Run

#### `pub unsafe fn spawn_unchecked<'a, F, T>(self, f: F) -> Result<JoinHandle<T>> where
F: FnOnce() -> T,
F: Send + 'a,
T: Send + 'a,` [src]

🔬 This is a nightly-only experimental API. (`thread_spawn_unchecked` #55132)

Spawns a new thread without any lifetime restrictions by taking ownership of the `Builder`, and returns an `io::Result` to its `JoinHandle`.

The spawned thread may outlive the caller (unless the caller thread is the main thread; the whole process is terminated when the main thread finishes). The join handle can be used to block on termination of the child thread, including recovering its panics.

This method is identical to `thread::Builder::spawn`, except for the relaxed lifetime bounds, which render it unsafe. For a more complete documentation see `thread::spawn`.

# Errors

Unlike the `spawn` free function, this method yields an `io::Result` to capture any failure to create the thread at the OS level.

# Panics

Panics if a thread name was set and it contained null bytes.

# Safety

The caller has to ensure that no references in the supplied thread closure or its return type can outlive the spawned thread's lifetime. This can be guaranteed in two ways:

- ensure that `join` is called before any referenced data is dropped
- use only types with `'static` lifetime bounds, i.e., those with no or only `'static` references (both `thread::Builder::spawn` and `thread::spawn` enforce this property statically)

# Examples

    #![feature(thread_spawn_unchecked)]
    use std::thread;

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

    let x = 1;
    let thread_x = &x;

    let handler = unsafe {
        builder.spawn_unchecked(move || {
            println!("x = {}", *thread_x);
        }).unwrap()
    };

    // caller has to ensure `join()` is called, otherwise
    // it is possible to access freed memory if `x` gets
    // dropped before the thread closure is executed!
    handler.join().unwrap();Run

## Trait Implementations

### `impl Debug for Builder`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

## Auto Trait Implementations

### `impl Send for Builder`

### `impl Sync for Builder`

## Blanket Implementations

### `impl<T> From for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `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, 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`)