Struct Builder
#[must_use = "must eventually spawn the thread"]
pub struct Builder { pub(in ::thread) name: Option<String>, pub(in ::thread) stack_size: Option<usize>, pub(in ::thread) no_hooks: bool }
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 threadstack_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 unwraps 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 thread;
let builder = new;
let handler = builder.spawn.unwrap;
handler.join.unwrap;
Fields
name: Option<String>A name for the thread-to-be, for identification in panic messages
stack_size: Option<usize>The size of the stack for the spawned thread in bytes
no_hooks: boolSkip running and inheriting the thread spawn hooks
Implementations
impl Builder
fn spawn_scoped<'scope, 'env, F, T>(self, scope: &'scope Scope<'scope, 'env>, f: F) -> Result<ScopedJoinHandle<'scope, T>> where F: FnOnce() -> T + Send + 'scope, T: Send + 'scope,Spawns a new scoped thread using the settings set through this
Builder.Unlike
Scope::spawn, this method yields anio::Resultto capture any failure to create the thread at the OS level.Panics
Panics if a thread name was set and it contained null bytes.
Example
use thread; let mut a = vec!; let mut x = 0; scope; // After the scope, we can modify and access our variables again: a.push; assert_eq!;
impl Builder
fn new() -> BuilderGenerates the base configuration for spawning a thread, from which configuration methods can be chained.
Examples
use thread; let builder = new .name .stack_size; let handler = builder.spawn.unwrap; handler.join.unwrap;fn name(self, name: String) -> BuilderNames 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 thread; let builder = new .name; let handler = builder.spawn.unwrap; handler.join.unwrap;fn stack_size(self, size: usize) -> BuilderSets 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 thread; let builder = new.stack_size;fn no_hooks(self) -> BuilderDisables running and inheriting spawn hooks.
Use this if the parent thread is in no way relevant for the child thread. For example, when lazily spawning threads for a thread pool.
fn spawn<F, T>(self, f: F) -> Result<JoinHandle<T>> where F: FnOnce() -> T + Send + 'static, T: Send + 'static,Spawns a new thread by taking ownership of the
Builder, and returns anio::Resultto itsJoinHandle.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 spawned thread, including recovering its panics.
For a more complete documentation see
thread::spawn.Errors
Unlike the
spawnfree function, this method yields anio::Resultto 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 thread; let builder = new; let handler = builder.spawn.unwrap; handler.join.unwrap;unsafe fn spawn_unchecked<F, T>(self, f: F) -> Result<JoinHandle<T>> where F: FnOnce() -> T + Send, T: Send,Spawns a new thread without any lifetime restrictions by taking ownership of the
Builder, and returns anio::Resultto itsJoinHandle.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 spawned thread, including recovering its panics.
This method is identical to [
thread::Builder::spawn]Builder::spawn, except for the relaxed lifetime bounds, which render it unsafe. For a more complete documentation seethread::spawn.Errors
Unlike the
spawnfree function, this method yields anio::Resultto 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 the spawned thread does not outlive any references in the supplied thread closure and its return type. This can be guaranteed in two ways:
- ensure that [
join]JoinHandle::joinis called before any referenced data is dropped - use only types with
'staticlifetime bounds, i.e., those with no or only'staticreferences (both [thread::Builder::spawn]Builder::spawnandthread::spawnenforce this property statically)
Examples
use thread; let builder = new; let x = 1; let thread_x = &x; let handler = unsafe ; // 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;- ensure that [
Trait Implementations
impl Debug for Builder
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Auto Trait Implementations
impl Freeze for Builder
impl RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl UnsafeUnpin for Builder
impl UnwindSafe for Builder
Blanket Implementations
impl<T> Any for Builder
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Builder
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Builder
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Builder
fn from(t: T) -> TReturns the argument unchanged.
impl<T> SizeHint for Builder
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for Builder
impl<T, U> Into<U> for Builder
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 Builder
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 Builder
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>