Struct threadgroup::ThreadGroup
[−]
[src]
pub struct ThreadGroup<T> { /* fields omitted */ }
Holds the collection of threads and the notification channel. All public functions operate on this struct.
Methods
impl<T> ThreadGroup<T>
[src]
fn new() -> ThreadGroup<T>
[src]
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();
fn spawn<F, R>(&mut self, f: F) where
F: FnOnce() -> T,
F: Send + 'static,
R: Send + 'static,
T: Send + 'static,
[src]
F: FnOnce() -> T,
F: Send + 'static,
R: Send + 'static,
T: Send + 'static,
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});
fn len(&self) -> usize
[src]
fn is_empty(&self) -> bool
[src]
Check if there is any thread not yet join()
ed.
fn join(&mut self) -> Result<T, JoinError>
[src]
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 !"), } }
fn join_timeout(&mut self, timeout: Duration) -> Result<T, JoinError>
[src]
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..."); } }