Crate threadgroup [] [src]

Manages a group of threads that all have the same return type and can be join()ed as a unit.

The implementation uses a mpsc channel internaly so that children (spawned threads) can notify the parent (owner of a ThreadGroup) that they are finished without the parent having to use a blocking std::thread::JoinHandle.join() call.

Examples

use std::thread::sleep;
use std::time::Duration;
use threadgroup::{JoinError, ThreadGroup};

// Initialize a group of threads returning `u32`.
let mut tg: ThreadGroup<u32> = ThreadGroup::new();

// Start a bunch of threads that'll return or panic after a while
tg.spawn::<_,u32>(|| {sleep(Duration::new(0,3000000));2});
tg.spawn::<_,u32>(|| {sleep(Duration::new(0,1500000));panic!()});
tg.spawn::<_,u32>(|| {sleep(Duration::new(10,0));3});
tg.spawn::<_,u32>(|| {sleep(Duration::new(0,1000000));1});

// Join them in the order they finished
assert_eq!(1,                   tg.join().unwrap());
assert_eq!(JoinError::Panicked, tg.join().unwrap_err());
assert_eq!(2,                   tg.join().unwrap());
assert_eq!(JoinError::Timeout,  tg.join_timeout(Duration::new(0,10000)).unwrap_err());

Structs

ThreadGroup

Holds the collection of threads and the notification channel. All public functions operate on this struct.

Enums

JoinError

Possible error returns from join() and join_timeout().