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
impl ThreadPoolBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates and returns a valid thread pool builder, but does not initialize it.
Sourcepub fn build(self) -> Result<ThreadPool, ThreadPoolBuildError>
pub fn build(self) -> Result<ThreadPool, ThreadPoolBuildError>
Creates a new ThreadPool initialized using this configuration.
Sourcepub fn thread_name<F>(self, closure: F) -> Self
pub fn thread_name<F>(self, closure: F) -> Self
Sets a closure which takes a thread index and returns the thread’s name.
Sourcepub const fn num_threads(self, num_threads: usize) -> Self
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.
Sourcepub const fn use_current_thread(self) -> Self
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.
Sourcepub const fn stack_size(self, stack_size: usize) -> Self
pub const fn stack_size(self, stack_size: usize) -> Self
Sets the stack size of the worker threads
Sourcepub fn start_handler<H>(self, start_handler: H) -> Self
pub fn start_handler<H>(self, start_handler: H) -> Self
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.