From c817629db067cdaa32ca45af651c92692cf96fca Mon Sep 17 00:00:00 2001 From: Shautvast Date: Mon, 9 Feb 2026 20:39:46 +0100 Subject: [PATCH] #20 threadpool finished --- src/main.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5fc6d36..9a4e4ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use log::info; use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; -use std::sync::{mpsc, Arc, Mutex}; +use std::sync::{Arc, Mutex, mpsc}; use std::{sync, thread}; static GET: &'static [u8] = b"GET / HTTP/1.1\r\n"; @@ -13,8 +13,11 @@ struct Worker { impl Worker { fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(|| { - receiver; + let thread = thread::spawn(move || { + loop { + let job = receiver.lock().unwrap().recv().unwrap(); + job(); + } }); Worker { id, thread } } @@ -43,7 +46,7 @@ impl ThreadPool { } } -struct Job {} +type Job = Box; fn main() -> std::io::Result<()> { env_logger::init();