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 WorkerStatefulPool::init. Access state through its owner from jobs executing on the pool, for example inside install_fn.

Worker access is backed by a thread-local RefCell. Keep with_worker and with_worker_mut closures short and non-yielding: if Rayon runs another job on the same thread while a borrow is active, re-entrant worker access can panic.

Independent WorkerStatefulPool owners can reuse the same threads with different state configurations; each owner clears its state when dropped.

The underlying rayon pool is created lazily on first access.

Implementations§

Source§

impl WorkerPool

Source

pub const fn new(num_threads: usize, thread_name_prefix: &'static str) -> Self

Creates a new lazy WorkerPool with the given number of threads and a thread name prefix.

The underlying rayon pool is not created until the first method that requires it is called. Thread names follow the pattern "{prefix}-{index:02}".

Source

pub fn is_initialized(&self) -> bool

Returns true if the underlying rayon pool has been initialized.

Source

pub const fn num_threads(&self) -> usize

Returns the number of threads the pool is configured with, without creating it.

Source

pub fn current_num_threads(&self) -> usize

Returns the total number of threads in the underlying rayon 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.

The closure runs with exclusive access to each selected thread’s worker. Only num_threads threads execute the closure; the rest skip it.

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_and_wait<R: Send + 'static>( &self, f: impl FnOnce() -> R + Send + 'static, ) -> R

Runs a closure on this pool, waiting for its result.

Unlike install_fn, this always queues the closure onto this pool when called from another rayon pool. This avoids Rayon running the closure on the caller’s worker through its cross-pool install path.

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

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

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

This borrows the thread-local worker for the entire duration of f. Do not yield to Rayon from inside f if another job could call with_worker_mut on the same thread. Yield points include parallel iterators, rayon::join, scopes, and waiting in ThreadPool::install on a different Rayon pool. A cross-pool install may run another job from the caller’s pool on the same thread while it waits.

Prefer copying or cloning the required worker state in f, returning from this method to release the borrow, and only then performing work that may yield.

§Panics

Panics if the current thread’s worker is already mutably borrowed, including by a re-entrant with_worker_mut call.

Source

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

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

This exclusively borrows the thread-local worker for the entire duration of f. The borrow is not re-entrant: if Rayon runs another job on the same thread before f returns, any call to with_worker or with_worker_mut from that job will panic.

Do not call operations that can yield to Rayon from inside f when re-entrant worker access is possible. This includes parallel iterators, rayon::join, scopes, and waiting in ThreadPool::install on a different Rayon pool. In particular, a cross-pool install cooperatively runs jobs from the caller’s pool while waiting for the target pool.

Prefer computing updates before entering with_worker_mut, then use this closure only to apply the update to the worker state.

§Panics

Panics if the current thread’s worker is already borrowed, including by a re-entrant with_worker or with_worker_mut call.

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> NonStaticAny for T
where T: ?Sized,

§

fn type_id(&self) -> TypeId

Returns the lifetime-erased type ID of self.
§

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
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

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