From f2ed92564c20de7d468881bbbf144b0af0de9c9a Mon Sep 17 00:00:00 2001 From: Shautvast Date: Thu, 12 Feb 2026 16:49:30 +0100 Subject: [PATCH] #21 call threadpool --- src/main.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9a4e4ec..ec3fd74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,14 @@ impl ThreadPool { } Self { workers, sender } } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + self.sender.send(job).unwrap(); + } } type Job = Box; @@ -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(()) }