recoverable_thread_pool/worker/
impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use super::r#type::Worker;
use crate::thread_pool::r#type::ThreadPoolJob;
use recoverable_spawn::*;
use std::sync::{mpsc::Receiver, Arc, Mutex};

impl Worker {
    #[inline]
    pub fn new(id: usize, receiver: Arc<Mutex<Receiver<ThreadPoolJob>>>) -> Worker {
        let thread: JoinHandle<()> = recoverable_spawn(move || loop {
            if let Ok(receiver_lock) = receiver.lock() {
                if let Ok(job) = receiver_lock.recv() {
                    job();
                }
            }
        });
        Worker {
            id,
            thread: Some(thread),
        }
    }
}