summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@sequoia-pgp.org>2022-04-10 11:29:30 +0300
committerLars Wirzenius <liw@sequoia-pgp.org>2022-04-10 11:29:30 +0300
commit4092f57f0768c54efc351e5fe22f7fe34f70edb4 (patch)
tree6f0d77dbff25c40e1be846272986f4dac534fc04
parent8794b6ba145e53113012b9102bed999957976a6a (diff)
downloadchecksums-4092f57f0768c54efc351e5fe22f7fe34f70edb4.tar.gz
add threads
Sponsored-by: author
-rw-r--r--src/main.rs29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index d0695af..b775127 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,8 @@
+use checksums::*;
use clap::Parser;
use rayon::prelude::*;
use std::path::PathBuf;
-use checksums::*;
+use std::thread::{spawn, JoinHandle};
fn main() {
let args = Args::parse();
@@ -16,6 +17,12 @@ fn main() {
eprintln!("ERROR: {e}");
std::process::exit(1);
}
+
+ println!();
+ if let Err(e) = checksums_threads(&args.filenames) {
+ eprintln!("ERROR: {e}");
+ std::process::exit(1);
+ }
}
fn checksums_linear(filenames: &[PathBuf]) -> anyhow::Result<()> {
@@ -24,7 +31,7 @@ fn checksums_linear(filenames: &[PathBuf]) -> anyhow::Result<()> {
.iter()
.map(|filename| checksum(filename))
.collect();
- print(&checksums?);
+ print_checksums(&checksums?);
Ok(())
}
@@ -34,6 +41,22 @@ fn checksums_rayon(filenames: &[PathBuf]) -> anyhow::Result<()> {
.par_iter()
.map(|filename| checksum(filename))
.collect();
- print(&checksums?);
+ print_checksums(&checksums?);
+ Ok(())
+}
+
+fn checksums_threads(filenames: &[PathBuf]) -> anyhow::Result<()> {
+ println!("threads");
+ let handles: Vec<JoinHandle<Result<FileChecksum, std::io::Error>>> = filenames
+ .iter()
+ .cloned()
+ .map(|filename| spawn(move || checksum(&filename.clone())))
+ .collect();
+ let mut checksums = vec![];
+ for handle in handles {
+ let result = handle.join().expect("thread join");
+ checksums.push(result?);
+ }
+ print_checksums(&checksums);
Ok(())
}