pub trait ThreadPool {
type ScopeRef<'s, 'env, 'scope>: Scope<'s, 'env, 'scope>
where 'scope: 's,
'env: 'scope + 's;
// Required methods
fn scope<'env, 'scope, F>(&'env self, f: F)
where for<'s> F: FnOnce(Self::ScopeRef<'s, 'env, 'scope>) + Send,
'env: 'scope;
fn max_num_threads(&self) -> NonZeroUsize;
// Provided method
fn run_all(&self, tasks: impl TaskQueue + Send) { ... }
}Expand description
Abstraction for parallel execution environments and thread pool management.
ThreadPool defines how parallel computations are executed on a set of worker threads.
Any type implementing this trait can serve as a thread pool for orx-parallel computations.
§Thread Count Decision
The actual number of threads used in a computation is determined by combining multiple configuration layers:
- Pool Layer (
max_num_threads()) - The thread pool’s maximum capacity - Environment Layer (
ORX_NUM_THREADS) - Global limit from environment variable - Computation Layer (
.num_threads()on Par) - Per-computation request - Input Size - Cannot exceed the number of input elements
The max_num_threads_for_computation() method implements this logic by returning the
minimum of all these constraints.
§Example
use orx_parallel::*;
// Pool setup: 8 threads requested, but env limits to 4
// ORX_NUM_THREADS=4 is set
let pool = Pool::once(8); // pool.max_num_threads() == 4
// Computation: request 6 threads on 100-element input
let result: Vec<_> = (0..100)
.into_par()
.map(|x| x * 2)
.pool(pool)
.num_threads(6)
.collect();
// Result: min(min(6, 100), 4) = 4 threads used§Implementations
Pool::basic- Persistent thread pool (default)Pool::once- Lightweight virtual pool, spawns threads on-demandPool::rayon- rayon thread pools
See the thread_usage.md documentation for a complete guide.
Required Associated Types§
Required Methods§
Sourcefn max_num_threads(&self) -> NonZeroUsize
fn max_num_threads(&self) -> NonZeroUsize
Returns the maximum number of threads available in the pool.
This value reflects all constraints applied up to pool creation time, including:
- The requested thread count from pool construction
- The
ORX_NUM_THREADSenvironment variable (if set) - The system’s available CPU cores
Individual computations can further limit this via .num_threads() method.
Provided Methods§
Sourcefn run_all(&self, tasks: impl TaskQueue + Send)
fn run_all(&self, tasks: impl TaskQueue + Send)
Runs all tasks in parallel on this pool.
tasks is a statically typed TaskQueue: pushed tasks are stored inline,
requiring no object safety, boxing or heap allocation. None of the tasks start
running until run_all is called.
Tasks can be created using the tasks! macro, or fluently via Tasks::new and push.
§Example
use orx_parallel::*;
let work_for = |n| std::thread::sleep(std::time::Duration::from_millis(n));
let tasks = tasks![
|| {
work_for(90);
println!("t1 completes 4th");
},
|| println!("t2 completes 1st"),
|| {
work_for(10);
println!("t3 completes 2nd");
},
|| {
work_for(50);
println!("t4 completes 3rd");
},
];
Pool::global().run_all(tasks);
// prints:
// t2 completes 1st
// t3 completes 2nd
// t4 completes 3rd
// t1 completes 4thBelow is a more practical example: computing independent statistics over the same input concurrently and collecting the results:
use orx_parallel::*;
use std::sync::Mutex;
let numbers = [4, 8, 15, 16, 23, 42];
let sum = Mutex::new(0);
let max = Mutex::new(i32::MIN);
let all_positive = Mutex::new(false);
let tasks = tasks![
|| *sum.lock().unwrap() = numbers.iter().sum(),
|| *max.lock().unwrap() = numbers.iter().copied().max().unwrap(),
|| *all_positive.lock().unwrap() = numbers.iter().all(|&x| x > 0),
];
Pool::global().run_all(tasks);
println!(
"sum={}, max={}, all_positive={}",
sum.into_inner().unwrap(),
max.into_inner().unwrap(),
all_positive.into_inner().unwrap(),
);Tasks can also be created fluently via Tasks::new:
use orx_parallel::*;
let tasks = Tasks::new()
.push(|| println!("task 1"))
.push(|| println!("task 2"));
Pool::global().run_all(tasks);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".