Crates.io | work_pool |
lib.rs | work_pool |
version | 0.1.0 |
source | src |
created_at | 2022-11-27 06:08:01.180338 |
updated_at | 2022-11-27 06:08:01.180338 |
description | A simple implementation of a work queue wrapped by a thread pool. |
homepage | |
repository | https://github.com/wilkua/work_pool/ |
max_upload_size | |
id | 723599 |
size | 9,688 |
A super simple implementation of a work queue wrapped by a thread pool.
The goal of this package is to have a quick way of creating a thread pool with a work queue backing for quick projects.
use std::sync::Arc;
use work_pool::WorkPool;
fn main() {
// Specify number of threads and work queue capacity
// Work queue capacity is workload based, but a good
// estimate might be 2 or 4 times the number of threads
let mut pool = WorkPool::new(8, 64);
// Set the executor function and star tthe work listener
pool.set_executor_and_start(|work| {
// Work is the data sent by `dispatch`
});
loop {
// do something, like get a TcpStream
let stream = accept_next_connection();
match stream {
Some(Ok(s)) => pool.dispatch(Arc::new(s)),
Some(Err(e)) => panic!(e),
None => break,
}
}
// Dropping the pool will send a Quit message to all active threads
and detach them. No joins happen here.
drop(pool);
}