The purpose of this program is to have one thread (main thread) working on I/O reading lines from a file and feeding it to a pool of worker threads whose job is to perform some processing on each line provided. In this case, the processing is running the String.contains() method.
Please note that as test input, I used a giant .txt file of English dictionary words found here:
Also note that you need to specify the file name of the file to be scanned as the sole command line argument to the program. I am mostly concerned with the threading model and multithreading implementation but welcome other feedback as well; new Rust programmer.
spmc crate ver is "0.2.2" for your cargo.toml
extern crate spmc;
use spmc::channel;
use std::thread;
use std::io::BufReader;
use std::fs::File;
use std::io::BufRead;
use std::u32::MAX;
use std::env;
use std::sync::Arc;
fn main() -> Result<(), std::io::Error>
{
let args: Vec<String> = env::args().collect();
match args.len()
{
2 => {},
_ => return Err(std::io::Error::new(std::io::ErrorKind::Other, "Incorrect number of args"))
}
let filename = &args[1] as &str;
let f1 = File::open(filename)?;
let mut br = BufReader::new(f1);
let mut vecData: Vec<String> = Vec::new();
let (tx, rx) = spmc::channel();
let mut handles = Vec::new();
for n in 0..5 {
let rx = rx.clone();
handles.push(thread::spawn(move || {
loop
{
let mut line_to_check: Arc<String> = rx.recv().unwrap();
if line_to_check.contains("test")
{
println!("HIT: {}", line_to_check);
}
}
}));
}
let mut input_str = String::new();
let mut bytes_read: usize = 1;
while bytes_read != 0 {
let mut is_copy = input_str.clone();
bytes_read =
match br.read_line(&mut is_copy)
{
Ok(num) => num,
Err(err) => return Err(std::io::Error::new(std::io::ErrorKind::Other, "read_line failed...\n"))
};
let str_arc : Arc<String> = Arc::new(is_copy);
tx.send(str_arc);
}
Ok(())
}