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]

[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

[src]

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

[src]

Queues a new job with a given priority.

A job can be a closure with no arguments and returns, or a type with Runnable trait. A queued job with highest priority runs at given time. The value for priority is totally relative. The constant NORMAL_PRIORITY (value = 0) can be used as reference.

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);

for i in -10..10 {
    pool.queue_with_priority(move || {
        // do some work
    }, jobpool::NORMAL_PRIORITY + i);
}
// ...
pool.shutdown(); // blocks until all jobs are done

[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

[src]

Get the number of current active worker threads.

[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

[src]

Shuts down this instance of JobPool without waiting for threads to finish.

This method will return all of the threads' JoinHandles (as few as initial JobPool size, and as many as additional active threads if auto_grow(...) has been used. It won't wait for the threads to finish, and it must be called explicitly. Once this method is called, this instance of JobPool will throw away jobs already in the queue while running the one at hand. 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();
    }
}

[src]

Check whether this JobPool instance has been shutdown.

Trait Implementations

impl Drop for JobPool
[src]

[src]

Executes the destructor for this type. Read more

Auto Trait Implementations

impl Send for JobPool

impl Sync for JobPool