crossbeam_utils::thread

Struct ScopedThreadBuilder

Source
pub struct ScopedThreadBuilder<'scope, 'env> { /* private fields */ }
Expand description

Configures the properties of a new thread.

The two configurable properties are:

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

The Scope::spawn method uses a builder with default configuration and unwraps its return value. You may want to use this builder when you want to recover from a failure to launch a thread.

§Examples

use crossbeam_utils::thread;

thread::scope(|s| {
    s.builder()
        .spawn(|_| println!("Running a child thread"))
        .unwrap();
}).unwrap();

Implementations§

Source§

impl<'scope, 'env> ScopedThreadBuilder<'scope, 'env>

Source

pub fn name(self, name: String) -> ScopedThreadBuilder<'scope, 'env>

Sets the name for the new thread.

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

For more information about named threads, see here.

§Examples
use crossbeam_utils::thread;
use std::thread::current;

thread::scope(|s| {
    s.builder()
        .name("my thread".to_string())
        .spawn(|_| assert_eq!(current().name(), Some("my thread")))
        .unwrap();
}).unwrap();
Source

pub fn stack_size(self, size: usize) -> ScopedThreadBuilder<'scope, 'env>

Sets the size of the stack for the new thread.

The stack size is measured in bytes.

For more information about the stack size for threads, see here.

§Examples
use crossbeam_utils::thread;

thread::scope(|s| {
    s.builder()
        .stack_size(32 * 1024)
        .spawn(|_| println!("Running a child thread"))
        .unwrap();
}).unwrap();
Source

pub fn spawn<F, T>(self, f: F) -> Result<ScopedJoinHandle<'scope, T>>
where F: FnOnce(&Scope<'env>) -> T + Send + 'env, T: Send + 'env,

Spawns a scoped thread with this configuration.

The scoped thread is passed a reference to this scope as an argument, which can be used for spawning nested threads.

The returned handle can be used to manually join the thread before the scope exits.

§Errors

Unlike the Scope::spawn method, 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 crossbeam_utils::thread;

thread::scope(|s| {
    let handle = s.builder()
        .spawn(|_| {
            println!("A child thread is running");
            42
        })
        .unwrap();

    // Join the thread and retrieve its result.
    let res = handle.join().unwrap();
    assert_eq!(res, 42);
}).unwrap();

Trait Implementations§

Source§

impl<'scope, 'env> Debug for ScopedThreadBuilder<'scope, 'env>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'scope, 'env> Freeze for ScopedThreadBuilder<'scope, 'env>

§

impl<'scope, 'env> RefUnwindSafe for ScopedThreadBuilder<'scope, 'env>

§

impl<'scope, 'env> Send for ScopedThreadBuilder<'scope, 'env>

§

impl<'scope, 'env> Sync for ScopedThreadBuilder<'scope, 'env>

§

impl<'scope, 'env> Unpin for ScopedThreadBuilder<'scope, 'env>

§

impl<'scope, 'env> UnwindSafe for ScopedThreadBuilder<'scope, 'env>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 56 bytes