pub struct Tasks;Expand description
Entry point for building a statically typed TaskQueue to run in parallel
via ThreadPool::run_all.
Since the queue is typed rather than relying on dynamic dispatch, pushed tasks are stored inline: no object safety, boxing or heap allocation is required.
§Example
use orx_parallel::*;
let work_for = |n| std::thread::sleep(std::time::Duration::from_millis(n));
let tasks = Tasks::new()
.push(|| {
work_for(90);
println!("t1 completes 4th");
})
.push(|| println!("t2 completes 1st"))
.push(|| {
work_for(10);
println!("t3 completes 2nd");
})
.push(|| {
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 built fluently via Tasks::new and TaskQueue::push.
Implementations§
Auto Trait Implementations§
impl Freeze for Tasks
impl RefUnwindSafe for Tasks
impl Send for Tasks
impl Sync for Tasks
impl Unpin for Tasks
impl UnsafeUnpin for Tasks
impl UnwindSafe for Tasks
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more