I have a question about the official tutorial on multithreaded cleaning

How to achieve,
Enter the "exit" command on the console to clean.
Or open "http://127.0.0.1:8080/terminal" and clean.

Complete source code

main.rs

mod lib;

use std::net::{TcpStream, TcpListener};
use std::io::{Read, Write};
use std::fs;
use std::thread;
use std::time::Duration;
use lib::ThreadPool;
use std::sync::{Arc, Mutex};

fn main() {
    let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
    {
        let thread_pool = ThreadPool::new(4);
        for stream in listener.incoming().take(4) {
            let stream = stream.unwrap();
            // thread::spawn(|| {
            //     handler_connection(stream);
            // });
            thread_pool.execute(|| {
                handler_connection(stream);
            })
        }
    }
    println!("Shutting down.");
}

fn handler_connection(mut stream: TcpStream,) {
    let mut buffer = vec![0; 1024];
    stream.read(&mut buffer).unwrap();
    let header = String::from_utf8_lossy(&buffer);
    // println!("buffer\n{}", header);

    let (file_name, status, ) =
        if header.starts_with("GET /terminal HTTP/1.1") {
            ("./terminal.html", "200 OK")
        } else if header.starts_with("GET /sleep HTTP/1.1") {
            thread::sleep(Duration::from_secs(5));
            ("./sleep.html", "200 OK")
        } else if header.starts_with("GET / HTTP/1.1") {
            ("./hello.html", "200 OK")
        } else {
            ("./404.html", "404 NOT FOUND")
        };

    let file_content = fs::read_to_string(file_name).unwrap();
    let response = format!("HTTP/1.1 {}\r\nConnect-length:{}\r\n\r\n{}",
                           status, file_content.len(), file_content);
    stream.write(response.as_bytes()).unwrap();
    stream.flush().unwrap();
}

lib.rs

use std::thread;
use std::sync::{mpsc, Mutex, Arc,};

type Job = Box<dyn FnOnce() + Send + 'static>;

enum Message {
    NewJob(Job),
    Terminate,
}

pub struct ThreadPool {
    workers: Vec<Worker>,
    sender: mpsc::Sender<Message>,
}

impl ThreadPool {
    pub fn new(size:usize) -> ThreadPool {
        assert!(size > 0);

        let (sender, receiver) = mpsc::channel();

        let locker = Mutex::new(receiver);
        let arc_locker = Arc::new(locker);
        let mut workers = Vec::with_capacity(size);
        for id in 0..size {
            workers.push(Worker::new(id, Arc::clone(&arc_locker)),);
        }

        ThreadPool {
            workers,
            sender: sender,
        }
    }

    pub fn execute<F>(&self, f: F)
        where F: FnOnce() + Send + 'static {
        let job = Box::new(f);
        let message = Message::NewJob(job);
        match self.sender.send(message) {
            Ok(_) => {

            },
            Err(e) => {
                panic!("error:\n{}", e)
            }
        }
    }
}

struct Worker {
    id: usize,
    thread: Option<thread::JoinHandle<()>>,
}

impl Worker {
    fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
        let thread = thread::spawn(move || {
            while let Ok(message) = {
                let x = receiver.lock().unwrap().recv();
                x
            } {
                let v = Worker::execute_message(id, message);
                if v == true {
                    break;
                }
            }
        });
        Worker {
            id,
            thread: Some(thread),
        }
    }

    fn execute_message(id:usize, message: Message) -> bool {
        match message {
            Message::NewJob(job) => {
                println!("Worker {} got a job; executing.", id);
                job();
                false
            },
            Message::Terminate => {
                println!("Worker {} was told to terminate.", id);
                true
            }
        }
    }
}

impl Drop for ThreadPool {
    fn drop(&mut self) {
        for _ in &self.workers {
            self.sender.send(Message::Terminate).unwrap();
        }

        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);
            if let Some(thread) = worker.thread.take() {
                thread.join().unwrap();
            }
        }
    }
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.