# HG changeset patch # User Floris Bruynooghe # Date 1562522158 -7200 # Sun Jul 07 19:55:58 2019 +0200 # Node ID ce53043ab0e85913fc57e3cee3271a20d37b2d68 # Parent 09a47d8379a5fc0bb9d86a865aa17947daf96db2 Add wait_timeout() method Not sure I'll end up using this though. diff --git a/src/pond.rs b/src/pond.rs --- a/src/pond.rs +++ b/src/pond.rs @@ -78,6 +78,25 @@ } } + pub fn wait_timeout(&mut self, dur: std::time::Duration) -> Option> { + let mutex = Arc::clone(&self.mutex); + let mut guard = mutex.lock().unwrap(); + let mut ret = self.reap_threads(); + while ret.is_none() { + let cvar_tuple = self.condv.wait_timeout(guard, dur).unwrap(); + guard = cvar_tuple.0; + ret = self.reap_threads(); + if ret.is_none() && cvar_tuple.1.timed_out() { + return None; + } + } + let thread = ret.unwrap(); + Some(TerminatedThread { + name: thread.name, + result: thread.joinhandle.join(), + }) + } + // This is O(n) for N being the number of threads. HashSet would // be O(1) but we assume N is low so this isn't worth it. // fn reap_threads(pond: &mut Pond) -> Option> {