Crates.io | workerpool-rs |
lib.rs | workerpool-rs |
version | 0.2.1 |
source | src |
created_at | 2020-10-10 22:27:15.251697 |
updated_at | 2023-05-05 17:41:24.769735 |
description | A simple workerpool for rust |
homepage | https://github.com/jcbritobr/workerpool |
repository | https://github.com/jcbritobr/workerpool |
max_upload_size | |
id | 298202 |
size | 11,027 |
A simple rust workerpool implementation that uses channels to synchronize the jobs. It can spawn a fixed number of worker threads, that waits for a job queue to consum.
use workerpool_rs::pool::WorkerPool;
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex};
let n_workers = 4;
let n_jobs = 8;
let pool = WorkerPool::new(n_workers);
let (tx, rx) = channel();
let atx = Arc::new(Mutex::new(tx));
for _ in 0..n_jobs {
let atx = atx.clone();
pool.execute(move|| {
let tx = atx.lock().unwrap();
tx.send(1).expect("channel will be there waiting for the pool");
});
}
assert_eq!(rx.iter().take(n_jobs).fold(0, |a, b| a + b), 8);
$ cargo test