Skip to main content

ThreadPool

Struct ThreadPool 

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

A work-stealing pool of worker threads.

Dropping the pool signals shutdown and joins every worker, so no task outlives it.

Implementations§

Source§

impl ThreadPool

Source

pub fn new(threads: usize) -> Result<Self>

Build a pool with threads workers.

threads is clamped to at least one. Use ThreadPool::default_threads for the machine’s parallelism.

§Errors

Returns PixelsError::Io if the operating system refuses to spawn a worker thread.

Source

pub fn default_threads() -> usize

The parallelism to use when the caller has no preference.

Falls back to one thread where the platform cannot report it, which yields a correct if serial engine rather than a failure.

Source

pub fn with_default_threads() -> Result<Self>

A pool sized to ThreadPool::default_threads.

§Errors

As ThreadPool::new.

Source

pub fn on_worker_thread() -> bool

Whether the calling thread is a worker of any pool.

A run blocks its caller until its tiles are done, so a run started from inside a task would hold a worker of the pool it is waiting on. Callers that would otherwise use a shared pool check this first.

Source

pub const fn threads(&self) -> usize

How many workers this pool runs.

Source

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

Queue task for execution.

Returns immediately. Panics inside task are contained by the worker and do not propagate here.

Source

pub fn run_all<F>(&self, tasks: Vec<F>) -> Result<()>
where F: FnOnce() -> Result<()> + Send + 'static,

Run tasks on the pool and return once every one has finished.

§Why 'static

Tasks must be 'static because they run on long-lived worker threads that outlive this call. Erasing a shorter lifetime is what rayon uses unsafe for, and ADR-0008 keeps unsafe_code = "forbid" in our crates. This costs the scheduler nothing: tiles are already Arc<TileBuf> and graph nodes Arc<Node>, so a task moves cheap handles rather than borrowing.

The calling thread blocks. Calling run_all from inside a pool task is not supported and can deadlock; the scheduler submits one flat batch per wave instead of nesting.

§Errors

Returns the failing task’s error, choosing the lowest-indexed failure when several fail. Which worker happens to fail first is a race; which task index is lowest is not, so the reported error is deterministic (SPEC §Guarantees 2).

Trait Implementations§

Source§

impl Debug for ThreadPool

Source§

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

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

impl Drop for ThreadPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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.