Hello all. First time working with multi-threading, so the set-up is directly from Building a Multi-Thread WebServer, in the book. However, repurposing it a little to try new things. I noticed that the threads (in my scenario) don't seem to run or work on the full word_list I am providing, which is a Fuzzing word list containing numbers 000
through 999
.
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
}
trait FnBox {
fn call_box(self: Box<Self>);
}
impl<F: FnOnce()>FnBox for F {
fn call_box(self: Box<F>) {
(*self)()
}
}
type Job = Box<dyn FnBox + Send + 'static>;
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id, Arc::clone(&receiver)));
}
ThreadPool {
workers,
sender,
}
}
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static
{
let job = Box::new(f);
self.sender.send(job).unwrap();
}
}
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move|| {
loop{
let job = receiver.lock().unwrap().recv().unwrap();
job.call_box();
}
});
Worker {
id,
thread,
}
}
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path> {
let file = File::open(filename)
.expect("Could not open word list.");
Ok(io::BufReader::new(file)
.lines())
}
pub fn run(properties: Properties) {
// let mut headers = HeaderMap::new();
// if properties._headers.len() >= 1 && properties._headers[0] != String::from("NONE") {
// headers = set_headers(properties._headers);
// };
compound(&properties._word_list[0]);
}
fn compound(word_list: &String) {
let pool = ThreadPool::new(5);
if let Ok(lines) = read_lines(word_list.as_str()) {
let mut counter = 0;
for line in lines {
counter += 1;
if let Ok(word) = line {
pool.execute(move|| {
println!("{:?}{:?}", word, counter);
});
}
}
}
}
Currently the threads are just printing out the word they are working on and an incrementing counter. But when I run the code it only ever shows that maybe 100 items were printed, and usually its 222 or before.
My thinking could be wrong here, because I have never worked with multi-threading before. Do the workers in the pool not remain active for incoming "jobs" in this scenario? I understand that the order will be all jarbled which is fine, but how can I get a set amount of workers to perform all the tasks?
Thanks in advance!