https://github.com/jgardona/rpools
A minimalist 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.
https://github.com/jgardona/rpools
concurrency library threadpool worker-pool
Last synced: 3 months ago
JSON representation
A minimalist 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.
- Host: GitHub
- URL: https://github.com/jgardona/rpools
- Owner: jgardona
- License: mit
- Created: 2023-12-01T11:05:00.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-13T16:58:06.000Z (about 2 years ago)
- Last Synced: 2025-09-03T03:02:01.891Z (4 months ago)
- Topics: concurrency, library, threadpool, worker-pool
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rpools
A minimalist 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.
## Install
```
$ cargo add rpools
```
## Usage
* **A simple workerpool**
```rust
use rpools::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();
// a long task goes here
// send results to channel (use it to sync the pool with the parent thread)
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);
```
* **Use sync module to synchronize your pool**
```rust
let njobs = 20;
let nworkers = 3;
let pool = pool::WorkerPool::new(nworkers);
let atomic = Arc::new(AtomicUsize::new(0));
let wg = WaitGroup::default();
// send the jobs to the pool
for _ in 0..njobs {
let wg = wg.clone();
let atomic = atomic.clone();
pool.execute(move || {
atomic.fetch_add(1, Ordering::Relaxed);
drop(wg);
});
}
// wait for the pool finnishes
wg.wait();
assert_eq!(njobs, atomic.load(Ordering::Relaxed));
```