WorkerPool

Struct WorkerPool 

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

Implements a continuous pool of rust threads thats doesn’t stops unless it gets out of scope.

Implementations§

Source§

impl WorkerPool

Source

pub fn new(size: usize) -> WorkerPool

Constructs a new WorkerPool of size x.

size: usize - Is the number of workers in WorkerPool object.
returns: a WorkerPool object.

§Examples
use workerpool_rs::pool::WorkerPool;

let pool = WorkerPool::new(3);

assert_eq!("workers[] = (id: 0)(id: 1)(id: 2)", pool.to_string());
Source

pub fn execute<J>(&self, f: J)
where J: FnOnce() + Send + Sync + 'static,

Executes a job. The job is moved to closure, as this function is FnOnce. \

f: A FnOnce closure hosted by a Box smart pointer.

§Examples
use workerpool_rs::pool::WorkerPool;
use std::sync::mpsc;
use std::sync::{Arc, Mutex};

let njobs = 20;
let nworkers = 10;

let pool = WorkerPool::new(nworkers);
let (tx, rx) = mpsc::channel();

let atx = Arc::new(Mutex::new(tx));

for _ in 0 .. njobs {
    let atx = atx.clone();
    pool.execute(move || {
        let tx = atx.lock().unwrap();
        tx.send(1).unwrap();
    });
}

let sum = rx.iter().take(njobs).sum();
assert_eq!(njobs, sum);

Trait Implementations§

Source§

impl Display for WorkerPool

Source§

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

Formats the value using the given formatter. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.