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
impl ThreadPool
Sourcepub fn new(threads: usize) -> Result<Self>
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.
Sourcepub fn default_threads() -> usize
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.
Sourcepub fn with_default_threads() -> Result<Self>
pub fn with_default_threads() -> Result<Self>
Sourcepub fn on_worker_thread() -> bool
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.
Sourcepub fn spawn(&self, task: impl FnOnce() + Send + 'static)
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.
Sourcepub fn run_all<F>(&self, tasks: Vec<F>) -> Result<()>
pub fn run_all<F>(&self, tasks: Vec<F>) -> Result<()>
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).