jobpool

Crates.iojobpool
lib.rsjobpool
version0.3.8
sourcesrc
created_at2017-09-27 01:28:37.66302
updated_at2018-04-19 01:08:25.85975
descriptionA simple and lightweight threadpool implementation
homepage
repositoryhttps://github.com/darshanparajuli/jobpool
max_upload_size
id33270
size29,707
Darshan Parajuli (darshanparajuli)

documentation

README

JobPool

A simple and lightweight threadpool implementation for Rust.

Build Status

Example

extern crate jobpool;

use jobpool::JobPool;
use std::thread;
use std::time::Duration;
use std::sync::mpsc;

fn main() {
    let pool_size = 8; // or number of cpu cores
    let mut pool = JobPool::new(pool_size);
    // pool.auto_grow(100);

    let (tx, rx) = mpsc::channel();

    for i in 0..100 {
        let tx = tx.clone();
        pool.queue(move || {
            // Do some work, following is just for example's sake
            thread::sleep(Duration::from_millis(100));
            println!("sending: {}", i);
            tx.send(i).unwrap();
        });
        // or pool.queue_with_priority(move || {...}, priority_val);
    }

    for _ in 0..100 {
        match rx.recv() {
            Ok(val) => println!("received: {}", val),
            Err(e) => eprintln!("Error: {}", e),
        }
    }

    // Explicit call to shutdown; JobPool shuts down automatically after
    // going out of scope as well.
    pool.shutdown();
}
Commit count: 125

cargo fmt