pub struct ThreadGroup<T> { /* private fields */ }
Expand description
Holds the collection of threads and the notification channel. All public functions operate on this struct.
Implementations§
Source§impl<T> ThreadGroup<T>
impl<T> ThreadGroup<T>
Sourcepub fn new() -> ThreadGroup<T>
pub fn new() -> ThreadGroup<T>
Initialize a group of threads returning T
.
§Examples
use threadgroup::ThreadGroup;
// spawning and joining require the struct to be mutable, and you'll need to provide type hints.
let mut tg: ThreadGroup<u32> = ThreadGroup::new();
Sourcepub fn spawn<F, R>(&mut self, f: F)
pub fn spawn<F, R>(&mut self, f: F)
Spawn a new thread, adding it to the ThreadGroup.
Operates like std::thread::spawn()
, but modifies the ThreadGroup instead of returning a JoinHandle
.
§Examples
use std::time::Duration;
use std::thread::sleep;
use threadgroup::{JoinError, ThreadGroup};
let mut tg: ThreadGroup<u32> = ThreadGroup::new();
tg.spawn::<_,u32>(|| {sleep(Duration::new(0,1000000));1});
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return count of threads that have been [spawn()
]ed but not yet [join()
]ed.
[spawn()
]: struct.ThreadGroup.html#method.spawn
[join()
]: struct.ThreadGroup.html#method.join
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if there is any thread not yet [join()
]ed.
[join()
]: struct.ThreadGroup.html#method.join
Sourcepub fn join(&mut self) -> Result<T, JoinError>
pub fn join(&mut self) -> Result<T, JoinError>
Join one thread of the ThreadGroup.
Operates like std::thread::JoinHandle.join()
, but picks the first thread that
terminates.
§Examples
use threadgroup::ThreadGroup;
let mut tg: ThreadGroup<u32> = ThreadGroup::new();
while !tg.is_empty() {
match tg.join() {
Ok(ret) => println!("Thread returned {}", ret),
Err(e) => panic!("Oh noes !"),
}
}
Sourcepub fn join_timeout(&mut self, timeout: Duration) -> Result<T, JoinError>
pub fn join_timeout(&mut self, timeout: Duration) -> Result<T, JoinError>
Try to join one thread of the ThreadGroup.
Operates like std::thread::JoinHandle.join()
, but picks the first thread that terminates
and gives up if the timeout is reached.
§Examples
use std::time::Duration;
use threadgroup::{JoinError, ThreadGroup};
let mut tg: ThreadGroup<u32> = ThreadGroup::new();
for _ in 0..10 {
if let Err(JoinError::Timeout) = tg.join_timeout(Duration::new(0,10000)) {
println!("Still working...");
}
}