Skip to main content

Tasks

Struct Tasks 

Source
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 4th

Below 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§

Source§

impl Tasks

Source

pub fn new() -> TasksEmpty

Creates a new, empty task queue to push tasks onto.

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> 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> SoM<T> for T

Source§

fn get_ref(&self) -> &T

Returns a reference to self.
Source§

fn get_mut(&mut self) -> &mut T

Returns a mutable reference to self.
Source§

impl<T> SoR<T> for T

Source§

fn get_ref(&self) -> &T

Returns a reference to 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.