Skip to main content

WorkerPool

Struct WorkerPool 

Source
pub struct WorkerPool { /* private fields */ }
Available on crate feature rayon only.
Expand description

A rayon thread pool with per-thread Worker state.

Each thread in the pool has its own Worker that can hold arbitrary state via Worker::init. The state is thread-local and accessible during install calls.

The pool supports multiple init/clear cycles, allowing reuse of the same threads with different state configurations.

Implementations§

Source§

impl WorkerPool

Source

pub fn new(num_threads: usize) -> Result<Self, ThreadPoolBuildError>

Creates a new WorkerPool with the given number of threads.

Source

pub fn from_builder( builder: ThreadPoolBuilder, ) -> Result<Self, ThreadPoolBuildError>

Creates a new WorkerPool from a [rayon::ThreadPoolBuilder].

Source

pub fn current_num_threads(&self) -> usize

Returns the total number of threads in the underlying rayon pool.

Source

pub fn init<T: 'static>(&self, f: impl Fn(Option<&mut T>) -> T + Sync)

Initializes per-thread Worker state on every thread in the pool.

Source

pub fn broadcast(&self, num_threads: usize, f: impl Fn(&mut Worker) + Sync)

Runs a closure on num_threads threads in the pool, giving mutable access to each thread’s Worker.

Use this to initialize or re-initialize per-thread state via Worker::init. Only num_threads threads execute the closure; the rest skip it.

Source

pub fn clear(&self)

Clears the state on every thread in the pool.

Source

pub fn install<R: Send>(&self, f: impl FnOnce(&Worker) -> R + Send) -> R

Runs a closure on the pool with access to the calling thread’s Worker.

All rayon parallelism (e.g. par_iter) spawned inside the closure executes on this pool. Each thread can access its own Worker via the provided reference or through additional WorkerPool::with_worker calls.

Source

pub fn install_fn<R: Send>(&self, f: impl FnOnce() -> R + Send) -> R

Runs a closure on the pool without worker state access.

Like install but for closures that don’t need per-thread Worker state.

Source

pub fn spawn(&self, f: impl FnOnce() + Send + 'static)

Spawns a closure on the pool.

Source

pub fn in_place_scope<'scope, R>( &self, f: impl FnOnce(&Scope<'scope>) -> R, ) -> R

Executes f on this pool using [rayon::in_place_scope], which converts the calling thread into a worker for the duration — tasks spawned inside the scope run on the pool and the call blocks until all of them complete.

Source

pub fn with_worker<R>(f: impl FnOnce(&Worker) -> R) -> R

Access the current thread’s Worker from within an install closure.

This is useful for accessing the worker from inside par_iter where the initial &Worker reference from install belongs to a different thread.

Source

pub fn with_worker_mut<R>(f: impl FnOnce(&mut Worker) -> R) -> R

Mutably access the current thread’s Worker from within a pool closure.

Trait Implementations§

Source§

impl Debug for WorkerPool

Source§

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

Formats the value using the given formatter. 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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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 = 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more

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: 8 bytes