Skip to content

Commit

Permalink
Instead of previous commit, join worker threads in drop
Browse files Browse the repository at this point in the history
Includes a test for whether any of them is the current thread
before joining.

Signed-off-by: Chris Beck <beck.ct@gmail.com>
  • Loading branch information
cbeck88 committed Apr 11, 2020
1 parent 154050d commit 452e447
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,6 @@ impl Environment {
let idx = self.idx.fetch_add(1, Ordering::Relaxed);
self.cqs[idx % self.cqs.len()].clone()
}

/// Shutdown the completion queues and join all threads
pub fn shutdown_and_join(&mut self) {
for cq in self.completion_queues() {
cq.shutdown();
}

for handle in self._handles.drain(..) {
handle.join().unwrap();
}
}
}

impl Drop for Environment {
Expand All @@ -148,6 +137,15 @@ impl Drop for Environment {
// it's safe to shutdown more than once.
cq.shutdown()
}

// Join our threads when we leave scope
// Try not to join the current thread
let current_thread_id = std::thread::current().id();
for handle in self._handles.drain(..) {
if handle.thread().id() != current_thread_id {
handle.join().unwrap();
}
}
}
}

Expand All @@ -174,6 +172,5 @@ mod tests {
}

assert_eq!(env.completion_queues().len(), 2);
env.shutdown_and_join();
}
}

0 comments on commit 452e447

Please sign in to comment.