Skip to main content

ThreadPoolBuilder

Struct ThreadPoolBuilder 

Source
pub struct ThreadPoolBuilder { /* private fields */ }
Expand description

Used to create a new ThreadPool.

§Examples

The following creates a thread pool with 4 threads.

use commonware_parallel::rayon::ThreadPoolBuilder;

let pool = ThreadPoolBuilder::new().num_threads(4).build().unwrap();
assert_eq!(pool.current_num_threads(), 4);
assert_eq!(pool.install(|| 1 + 1), 2);

Implementations§

Source§

impl ThreadPoolBuilder

Source

pub fn new() -> Self

Creates and returns a valid thread pool builder, but does not initialize it.

Source

pub fn build(self) -> Result<ThreadPool, ThreadPoolBuildError>

Creates a new ThreadPool initialized using this configuration.

Source

pub fn thread_name<F>(self, closure: F) -> Self
where F: FnMut(usize) -> String + 'static,

Sets a closure which takes a thread index and returns the thread’s name.

Source

pub const fn num_threads(self, num_threads: usize) -> Self

Sets the number of threads to be used in the thread pool.

If you specify a non-zero number of threads using this function, then the resulting thread pools are guaranteed to start at most this number of threads.

If num_threads is 0, or you do not call this function, then the pool uses RAYON_NUM_THREADS, then the deprecated RAYON_RS_NUM_CPUS, and otherwise the number of logical CPUs reported by std::thread::available_parallelism. A zero RAYON_NUM_THREADS selects the logical CPU count.

Source

pub const fn use_current_thread(self) -> Self

Use the current thread as one of the threads in the pool.

The current thread is guaranteed to be at index 0, and since the thread is not managed by the pool, the start handler does not run for that thread.

Note that the current thread won’t run the main work-stealing loop, so jobs spawned into the thread pool will generally not be picked up automatically by this thread unless you yield to the pool in some way, like via the pool’s yield_now.

§Leaks

The current thread keeps its worker state for the rest of its life, so the pool’s registry is leaked.

Source

pub const fn stack_size(self, stack_size: usize) -> Self

Sets the stack size of the worker threads

Source

pub fn start_handler<H>(self, start_handler: H) -> Self
where H: Fn(usize) + Send + Sync + 'static,

Sets a callback to be invoked on thread start.

The closure is passed the index of the thread on which it is invoked. Note that this same closure may be invoked multiple times in parallel. If this closure panics, the process aborts.

Trait Implementations§

Source§

impl Debug for ThreadPoolBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ThreadPoolBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.