Struct jobpool::JobPool
[−]
[src]
pub struct JobPool { /* fields omitted */ }
JobPool manages a job queue to be run on a specified number of threads.
Methods
impl JobPool
[src]
fn new(size: usize) -> Self
[src]
Creates a new job pool.
Using the number of cpu cores as the argument for size
is recommended.
Higher values can result iSeqCstn larger memory footprint,
and non-optimal performance.
Panics
This function will panic if the argument for size
is 0.
Examples
use jobpool::JobPool; let pool_size: usize = 8; // number of cpu cores is recommended let mut pool = JobPool::new(pool_size); pool.queue(|| { // do some work }); // ... pool.shutdown(); // blocks until all jobs are done
fn queue<J>(&mut self, job: J) where
J: Runnable + Send + 'static,
[src]
J: Runnable + Send + 'static,
Queues a new "job".
A "job" can be a closure with no arguments and returns, or
a type with Runnable
trait. A queued job gets run in a first-come, first-serve basis.
Panics
This method will panic if the JobPool instance has already been shutdown.
Examples
use jobpool::JobPool; let pool_size: usize = 8; // number of cpu cores is recommended let mut pool = JobPool::new(pool_size); pool.queue(|| { // do some work }); // ... pool.shutdown(); // blocks until all jobs are done
fn auto_grow(&mut self, max_size: usize)
[src]
Automatically increase the number of worker threads as needed until max_size
is reached.
Panics
This method will panic if max_size
is less than or equal to initial JobPool size.
Examples
use jobpool::JobPool; let pool_size: usize = 8; // number of cpu cores is recommended let mut pool = JobPool::new(pool_size); pool.auto_grow(100); for _ in 0..1000 { pool.queue(|| { // do some work }); } // ... pool.shutdown(); // blocks until all jobs are done
fn active_workers_count(&self) -> usize
[src]
Get the number of current active worker threads.
fn shutdown(&mut self)
[src]
Shuts down this instance of JobPool.
This method will wait for all of the queued jobs to finish. It also gets called automatically as the instance goes out of scope, so calling this method can be optional.
Examples
use jobpool::JobPool; let pool_size: usize = 8; // number of cpu cores is recommended let mut pool = JobPool::new(pool_size); pool.queue(|| { // do some work }); // ... pool.shutdown(); // blocks until all jobs are done
fn shutdown_no_wait(&mut self) -> Option<Vec<JoinHandle<()>>>
[src]
Shuts down this instance of JobPool without waiting for threads to finish.
This method will return all of the threads' JoinHandle
s.
It won't wait for the threads to finish, and it must be called explicitly.
Calling shutdown()
after this method won't have any effect.
Unlike shutdown()
, it doesn't get called automatically after going out of scope.
Examples
use jobpool::JobPool; let pool_size: usize = 8; // number of cpu cores is recommended let mut pool = JobPool::new(pool_size); pool.queue(|| { // do some work }); // ... let handles = pool.shutdown_no_wait(); // ... if let Some(handles) = handles { for handle in handles { let _ = handle.join(); } }