Skip to content

Commit ce49a88

Browse files
author
aligator
committed
fixed stopping threads
1 parent 1d2e805 commit ce49a88

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/exp.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ impl Manager {
6868
self.work_tx.send(job);
6969
}
7070

71-
pub fn wait(&self) -> usize {
71+
pub fn stop(&self) -> usize {
72+
// send empty string for each worker (empty string is command for closing)
73+
for i in 0..self.pool_size {
74+
self.work_tx.send(String::new());
75+
}
76+
7277
loop {
7378
if self.worker_finish_rx.len() == self.pool_size {
7479
break;
@@ -88,7 +93,9 @@ impl Scanner {
8893
let path = item.path().display().to_string();
8994
mg.recv(path);
9095
}
91-
}
96+
};
97+
98+
println!("test");
9299
Ok(())
93100
}
94101
}
@@ -103,8 +110,13 @@ struct Worker {
103110
impl Worker {
104111
fn reicv(&self, work_rx: cc::Receiver<String>, finished: cc::Sender<bool>) {
105112
loop {
106-
match work_rx.try_recv() {
113+
match work_rx.recv() {
107114
Ok(job) => {
115+
// empty string is signal for closing worker
116+
if job.is_empty() {
117+
break;
118+
}
119+
108120
let mut handle = match File::open(Path::new(&job)) {
109121
Ok(h) => h,
110122
Err(err) => {
@@ -118,17 +130,16 @@ impl Worker {
118130
let positive = self.process(&mut reader, &self.term);
119131

120132
if positive {
121-
println!("Found in file {}", job);
133+
println!("Found in file {} {}", job, self.counter.get());
122134
self.counter.inc();
123135
}
124136
},
125137
Err(e) => {
126-
if e.is_disconnected() {
127-
break;
128-
}
138+
break;
129139
},
130140
}
131-
}
141+
};
142+
println!("w finish");
132143
finished.send(true).unwrap();
133144
}
134145

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,5 @@ fn run_exp(dir: &str, term: &str, pool_size: usize, trim_size: usize) -> Result<
6565
let dir = dir.to_owned();
6666
let _ = exp::Scanner{}.run(dir, &haystack);
6767

68-
haystack.wait();
69-
Ok(1)
68+
Ok(haystack.stop())
7069
}

0 commit comments

Comments
 (0)