#20 threadpool finished

This commit is contained in:
Shautvast 2026-02-09 20:39:46 +01:00
parent 35e2a66628
commit c817629db0

View file

@ -1,7 +1,7 @@
use log::info; use log::info;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream}; use std::net::{TcpListener, TcpStream};
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{Arc, Mutex, mpsc};
use std::{sync, thread}; use std::{sync, thread};
static GET: &'static [u8] = b"GET / HTTP/1.1\r\n"; static GET: &'static [u8] = b"GET / HTTP/1.1\r\n";
@ -13,8 +13,11 @@ struct Worker {
impl Worker { impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker { fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(|| { let thread = thread::spawn(move || {
receiver; loop {
let job = receiver.lock().unwrap().recv().unwrap();
job();
}
}); });
Worker { id, thread } Worker { id, thread }
} }
@ -43,7 +46,7 @@ impl ThreadPool {
} }
} }
struct Job {} type Job = Box<dyn FnOnce() + Send + 'static>;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
env_logger::init(); env_logger::init();