https://github.com/darshanparajuli/jobpool
A simple and lightweight threadpool library for Rust.
https://github.com/darshanparajuli/jobpool
concurrency rust rust-library threading threadpool
Last synced: about 1 year ago
JSON representation
A simple and lightweight threadpool library for Rust.
- Host: GitHub
- URL: https://github.com/darshanparajuli/jobpool
- Owner: darshanparajuli
- License: mit
- Created: 2017-09-27T01:21:07.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-03-16T04:06:58.000Z (about 6 years ago)
- Last Synced: 2025-03-24T14:46:28.262Z (about 1 year ago)
- Topics: concurrency, rust, rust-library, threading, threadpool
- Language: Rust
- Homepage:
- Size: 65.4 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JobPool
A simple and lightweight threadpool implementation for Rust.
[](https://travis-ci.org/darshanparajuli/jobpool)
## Example
```rust
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();
}
```