#21 call threadpool

This commit is contained in:
Shautvast 2026-02-12 16:49:30 +01:00
parent c817629db0
commit f2ed92564c

View file

@ -44,6 +44,14 @@ impl ThreadPool {
}
Self { workers, sender }
}
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
let job = Box::new(f);
self.sender.send(job).unwrap();
}
}
type Job = Box<dyn FnOnce() + Send + 'static>;
@ -54,9 +62,7 @@ fn main() -> std::io::Result<()> {
let pool = ThreadPool::new(4);
for stream in listener.incoming() {
let stream = stream?;
thread::spawn(|| {
handle_connection(stream).unwrap();
});
pool.execute(|| handle_connection(stream).unwrap());
}
Ok(())
}