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
impl WorkerPool
Sourcepub fn new(size: usize) -> WorkerPool
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());Sourcepub fn execute<J>(&self, f: J)
pub fn execute<J>(&self, f: J)
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§
Auto Trait Implementations§
impl Freeze for WorkerPool
impl !RefUnwindSafe for WorkerPool
impl Send for WorkerPool
impl Sync for WorkerPool
impl Unpin for WorkerPool
impl !UnwindSafe for WorkerPool
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