Rust 程式設計小專案:WebServer 06

linghuyichong發表於2020-04-04

影片地址

頭條地址:https://www.ixigua.com/i680718929763709798...
B站地址:https://www.bilibili.com/video/BV177411m78...

github地址

https://github.com/anonymousGiga/web-serve...

在之前用執行緒池實現的web server中,每個工作執行緒中透過loop進行迴圈,從channel的接收端等待任務,然後執行。但是程式碼中沒有提供一種機制,來通知這些工作執行緒結束。本節就是在之前的基礎上,來實現執行緒池物件的正確清除。

透過為ThreadPool實現Drop trait來實現執行緒池物件清除

修改Worker如下:

struct Worker {
    id: usize,
    thread: Option<thread::JoinHandle<()>>,
}
impl Worker {
    fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
        // --snip--

        Worker {
            id,
            thread: Some(thread),
        }
    }
}

為ThreadPool實現Drop:

impl Drop for ThreadPool {
    fn drop(&mut self) {
        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);

            if let Some(thread) = worker.thread.take() {//知識點:Option的take方法
                thread.join().unwrap();
            }
        }
    }
}

通知worker執行緒結束

修改傳送內容的格式:

enum Message {
    NewJob(Job),
    Terminate,
}

修改ThreadPool:

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

// --snip--

impl ThreadPool {
    // --snip--

    pub fn execute<F>(&self, f: F)
        where
            F: FnOnce() + Send + 'static
    {
        let job = Box::new(f);

        self.sender.send(Message::NewJob(job)).unwrap();
    }
}

修改Worker:

impl Worker {
    fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) ->
        Worker {

        let thread = thread::spawn(move ||{
            loop {
                let message = receiver.lock().unwrap().recv().unwrap();

                match message {
                    Message::NewJob(job) => {
                        println!("Worker {} got a job; executing.", id);

                        job();
                    },
                    Message::Terminate => {
                        println!("Worker {} was told to terminate.", id);

                        break;
                    },
                }
            }
        });

        Worker {
            id,
            thread: Some(thread),
        }
    }
}

再在ThreadPool的Drop實現中新增傳送結束資訊:

impl Drop for ThreadPool {
    fn drop(&mut self) {
        println!("Sending terminate message to all workers.");

        for _ in &mut self.workers {
            self.sender.send(Message::Terminate).unwrap();
        }

        println!("Shutting down all workers.");

        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);

            if let Some(thread) = worker.thread.take() {
                thread.join().unwrap();
            }
        }
    }
}

修改主函式:

//src/main.rs
fn main() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    let pool = ThreadPool::new(4);

    for stream in listener.incoming().take(2) {//take 方法定義於 Iterator trait,這裡限制迴圈最多頭 2 次
        let stream = stream.unwrap();

        pool.execute(|| {
            handle_connection(stream);
        });
    }

    println!("Shutting down.");
}

總結

web server這個例子來自於The Rust這本教材,透過這個例子,我們能基本對如何用Rust寫一個簡單的執行緒池有一個瞭解,用到的知識點主要是channel、thread、Arc等知識點。

後續我們還將為大家寫一個簡單的區塊鏈的例子。

關注令狐一衝,關注區塊鏈和Rust

本作品採用《CC 協議》,轉載必須註明作者和本文連結
令狐一衝

相關文章