tinypool 0.2.0

A simple thread pool implementation in Rust
Documentation
  • Coverage
  • 100%
    11 out of 11 items documented8 out of 10 items with examples
  • Size
  • Source code size: 35.21 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.84 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • amamic1803/tinypool-rs
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • amamic1803

tinypool-rs

tinypool is a simple thread pool implementation in Rust.

A thread pool is a collection of threads that can be used to execute jobs concurrently. The aim of this crate is to provide a simple, easy-to-use thread pool that can be used in any project.

Documentation

Crate

Example (1)

use tinypool::ThreadPool;
use std::sync::{Arc, Mutex};

let mut threadpool = ThreadPool::new(Some(4)).unwrap();
let counter = Arc::new(Mutex::new(0));

for _ in 0..100 {
    let counter_thrd = Arc::clone(&counter);
    threadpool.add_to_queue(move || {
        let mut counter = counter_thrd.lock().unwrap();
        *counter += 1;
    });
}

threadpool.join();
assert_eq!(*counter.lock().unwrap(), 100);

Example (2)

use tinypool::ThreadPool;
use std::sync::mpsc;

let mut threadpool = ThreadPool::new(Some(4)).unwrap();
let (tx, rx) = mpsc::channel();

for i in 0..100 {
    let tx = tx.clone();
    threadpool.add_to_queue(move || tx.send(i).unwrap());
}
drop(tx);

let mut sum = 0;
while let Ok(i) = rx.recv() {
   sum += i;
}

assert_eq!(sum, 4950);