SHARE
TWEET

Untitled

a guest Apr 25th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::thread;
  2. use std::sync::mpsc;
  3. use std::sync::Arc;
  4. use std::sync::Mutex;
  5. use std::time::Duration;
  6.  
  7. fn main(){
  8.     let pool1 = ThreadPool::new(1);
  9.     pool1.execute(|| {
  10.             print!{"test"};
  11.         });
  12.         pool1.execute(|| {
  13.             println!{"test1"};
  14.         });
  15.  
  16.         pool1.execute(|| {
  17.             loop{
  18.                 print!{"."};
  19.             };
  20.         });
  21.         pool1.execute(|| {
  22.             println!{"test2"};
  23.             println!{"test22"};
  24.             println!{"test2222"};
  25.         });
  26.        
  27.        
  28.    
  29.    
  30. }
  31.  
  32. pub struct ThreadPool {
  33.     workers: Vec<Worker>,
  34.     sender: mpsc::Sender<Message>,
  35. }
  36.  
  37. trait FnBox {
  38.     fn call_box(self: Box<Self>);
  39. }
  40.  
  41. impl<F: FnOnce()> FnBox for F {
  42.     fn call_box(self: Box<F>) {
  43.         (*self)();
  44.     }
  45. }  
  46.  
  47. type Job = Box<FnBox + Send + 'static>;
  48.  
  49. impl ThreadPool {
  50.     pub fn new(size: usize) -> ThreadPool {
  51.  
  52.         assert!(size > 0);
  53.         let (sender, receiver) = mpsc::channel();
  54.         let receiver = Arc::new(Mutex::new(receiver));
  55.         let mut workers = Vec::with_capacity(size);
  56.         for id in 0..size {
  57.             workers.push(Worker::new(id, Arc::clone(&receiver)));
  58.         }
  59.  
  60.         ThreadPool {
  61.             workers,
  62.             sender,
  63.         }
  64.     }
  65.  
  66.     pub fn execute<F>(&self, f: F)
  67.         where
  68.             F: FnOnce() + Send + 'static
  69.     {
  70.  
  71.         let job = Box::new(f);
  72.  
  73.         self.sender.send(Message::NewJob(job)).unwrap();
  74.  
  75.     }
  76. }
  77.  
  78. impl Drop for ThreadPool {
  79.     fn drop(&mut self) {
  80.         println!("Sending terminate to all workers");
  81.  
  82.         for _ in &mut self.workers {
  83.             self.sender.send(Message::Terminate).unwrap();
  84.         }
  85.  
  86.         println!("Shutting down all workers");
  87.  
  88.         for worker in &mut self.workers {
  89.             println!("Shutting down worker {}", worker.id);
  90.  
  91.             if let Some(thread) = worker.thread.take() {
  92.                 thread.join().unwrap();
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. struct Worker {
  99.     id: usize,
  100.     thread: Option<thread::JoinHandle<()>>,
  101. }
  102.  
  103. impl Worker {
  104.  
  105.     fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
  106.         let thread = thread::spawn(move || {
  107.             loop {
  108.                 let message = receiver.lock().unwrap().recv().unwrap();
  109.  
  110.                 match message {
  111.                     Message::NewJob(job) => {
  112.  
  113.                         println!("Worker {} got a job; executing.", id);
  114.  
  115.                         job.call_box();
  116.                     },
  117.                     Message::Terminate => {
  118.                         println!("Worker {} was told to terminate", id);
  119.                         break;
  120.                     },
  121.                 }
  122.  
  123.             }
  124.         });
  125.  
  126.         Worker {
  127.             id,
  128.             thread: Some(thread),
  129.         }
  130.     }
  131.  
  132. }
  133.  
  134. enum Message {
  135.     NewJob(Job),
  136.     Terminate,
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top