1 个不稳定版本
0.1.0 | 2022年11月27日 |
---|
982 在 并发 中
8KB
159 行
WorkPool
线程池包装的工作队列的超简单实现。
此包的目标是提供一种快速创建带工作队列的线程池的方法,适用于快速项目。
示例
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);
}