From 1645992a7fb5e04a0ffb8c90a2cbca6e76d95ffd Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 2 Apr 2020 09:34:11 -0700 Subject: [PATCH] Instead of previous commit, `join` worker threads in `drop` Includes a test for whether any of them is the current thread before joining. --- src/env.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/env.rs b/src/env.rs index 1a256fef7..7f2871731 100644 --- a/src/env.rs +++ b/src/env.rs @@ -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 { @@ -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 env._handles.drain(..) { + if handle.thread().id() != current_thread_id { + handle.join().unwrap(); + } + } } } @@ -174,6 +172,5 @@ mod tests { } assert_eq!(env.completion_queues().len(), 2); - env.shutdown_and_join(); } }