diff --git a/src/env.rs b/src/env.rs index 799a0adb3..a0cea0901 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 self._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(); } }