4 releases

0.1.3 Nov 5, 2024
0.1.2 Nov 3, 2024
0.1.1 Nov 2, 2024
0.1.0 Nov 2, 2024

#4 in #executing

Download history 348/week @ 2024-11-02 15/week @ 2024-11-09

363 downloads per month
Used in 2 crates (via asynk)

MIT/Apache

10KB
244 lines

tpool

A "lazy" thread pool for executing tasks. This thread pool only creates new threads when there are no idle threads available to handle incoming jobs. It aims to be efficient by minimizing the number of threads and only spawning new ones as needed.

Example

use tpool::ThreadPool;
use std::sync::mpsc

fn main() {
    let thread_count = 8;

    let tp = ThreadPool::new(thread_count.try_into().unwrap());

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

    for _ in 0..thread_count {
        let tx = tx.clone();
        tp.spawn(move || {
            tx.send(1).unwrap();
        });
    }

    assert_eq!(rx.iter().take(thread_count).sum::<usize>(), thread_count);
}

Dependencies

~0.4–5.5MB
~11K SLoC