26 releases

Uses old Rust 2015

0.3.8 Apr 19, 2018
0.3.7 Apr 18, 2018
0.3.3 Dec 2, 2017
0.3.2 Nov 6, 2017
0.1.10 Sep 28, 2017

#183 in Concurrency

Download history 2/week @ 2021-06-02 29/week @ 2021-06-09 2/week @ 2021-06-16 3/week @ 2021-06-23 2/week @ 2021-06-30 2/week @ 2021-07-07 27/week @ 2021-07-14 2/week @ 2021-07-21 53/week @ 2021-07-28 30/week @ 2021-08-04 28/week @ 2021-08-11 1/week @ 2021-08-18 2/week @ 2021-08-25 3/week @ 2021-09-08 1/week @ 2021-09-15

133 downloads per month
Used in 2 crates (via tange)

MIT license

25KB
522 lines

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

No runtime deps