fimxi 0.1.0

A multithreaded actor pattern framework
Documentation
use std::sync::mpsc;
use std::sync::Arc;
use std::sync::Mutex;

use crate::thread::Thread;
use crate::actor::Actor;

pub struct ThreadPool {
    threads: Vec<Thread>,
    sender: mpsc::Sender<Actor>
}

impl ThreadPool {
    /// Create a new threadPool.
    ///
    /// The size is the number of threads in the pool.
    ///
    /// # Panics
    ///
    /// The `new` function will panic if the size is zero.
    pub fn new(size: usize) -> ThreadPool {
        assert!(size > 0);
        let (sender, receiver) = mpsc::channel();
        let receiver = Arc::new(Mutex::new(receiver));
        let mut threads = Vec::with_capacity(size);
        for id in 0..size {
            threads.push(Thread::new(id, Arc::clone(&receiver)));
        }
        ThreadPool {
            threads,
            sender
        }
    }    
    pub fn execute<F>(&self, f:F) 
        where
            F: FnOnce() + Send + 'static
        {
            let actor = Box::new(f);
            self.sender.send(actor).unwrap();
        }
}