Still broken but it compiles. For some reason lines are not properly being read and only one thread is being run
This commit is contained in:
parent
65df621cf6
commit
3ffed9099c
@ -2,7 +2,7 @@ use std::{
|
|||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{BufRead, BufReader},
|
io::{BufRead, BufReader},
|
||||||
sync::{Arc, Mutex, MutexGuard},
|
sync::{Arc, Mutex},
|
||||||
thread,
|
thread,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -20,22 +20,19 @@ fn main() {
|
|||||||
let cores: usize = std::thread::available_parallelism().unwrap().into();
|
let cores: usize = std::thread::available_parallelism().unwrap().into();
|
||||||
|
|
||||||
let file = File::open("../measurements.txt").expect("File measurements.txt not found");
|
let file = File::open("../measurements.txt").expect("File measurements.txt not found");
|
||||||
let reader = BufReader::new(file);
|
|
||||||
let lines = Arc::new(Mutex::new(reader.lines()));
|
|
||||||
let chunk_length = 1_000_000_000 / cores;
|
let chunk_length = 1_000_000_000 / cores;
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for _i in 0..cores {
|
for i in 0..cores {
|
||||||
|
let reader = BufReader::new(file.try_clone().unwrap());
|
||||||
|
let line_chunk = reader.lines().skip(chunk_length * i).take(chunk_length);
|
||||||
let stations_clone = stations.clone();
|
let stations_clone = stations.clone();
|
||||||
let lines_clone = lines.clone();
|
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
let lines_guard = lines_clone.lock().unwrap();
|
for line in line_chunk {
|
||||||
let chunk = lines_guard.take(chunk_length);
|
let line = line.expect("could not read line");
|
||||||
lines_guard.skip(chunk_length);
|
println!("Thread #{i}: {line}");
|
||||||
for line_result in chunk {
|
let (station, temp) = line.split_once(';').expect("Error while splitting");
|
||||||
let line = line_result.expect("could not read line");
|
let temp = temp.parse().expect("Error while parsing temperature");
|
||||||
let (station, temp) = line.split_once(';').unwrap();
|
let mut stations_guard = stations_clone.lock().expect("Error while locking thread");
|
||||||
let temp = temp.parse().unwrap();
|
|
||||||
let mut stations_guard = stations_clone.lock().unwrap();
|
|
||||||
let measurements_option = stations_guard.get_mut(station);
|
let measurements_option = stations_guard.get_mut(station);
|
||||||
if let Some(measurements) = measurements_option {
|
if let Some(measurements) = measurements_option {
|
||||||
if temp < measurements.min {
|
if temp < measurements.min {
|
||||||
@ -52,10 +49,7 @@ fn main() {
|
|||||||
count: 1,
|
count: 1,
|
||||||
sum: temp,
|
sum: temp,
|
||||||
};
|
};
|
||||||
stations_clone
|
stations_guard.insert(station.to_owned(), measurements);
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.insert(station.to_owned(), measurements);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user