Struct ThreadGroup

Source
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>

Source

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();
Source

pub fn spawn<F, R>(&mut self, f: F)
where F: FnOnce() -> T + 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});
Source

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

Source

pub fn is_empty(&self) -> bool

Check if there is any thread not yet [join()]ed. [join()]: struct.ThreadGroup.html#method.join

Source

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 !"),
    }
}
Source

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...");
   }
}

Auto Trait Implementations§

§

impl<T> Freeze for ThreadGroup<T>

§

impl<T> !RefUnwindSafe for ThreadGroup<T>

§

impl<T> Send for ThreadGroup<T>

§

impl<T> !Sync for ThreadGroup<T>

§

impl<T> Unpin for ThreadGroup<T>

§

impl<T> !UnwindSafe for ThreadGroup<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.