Trait GenericThreadpool

Source
pub trait GenericThreadpool<'scope, I, O>:
    IntoIterator<Item = O>
    + Extend<I>
    + Sized
    + Send
    + Sync {
    type Iter<'a>: Iterator<Item = O> + 'a
       where Self: 'a;
    type JoinHandle;

    // Required methods
    fn submit(&self, input: I);
    fn recv(&self) -> O;
    fn try_recv(&self) -> Option<O>;
    fn iter(&self) -> Self::Iter<'_>;
    fn wait_until_finished(&self);
    fn producer<T>(&self, iter: T) -> Self::JoinHandle
       where T: IntoIterator<Item = I> + Send + Sync + 'scope;
    fn consumer<F>(&self, f: F) -> Self::JoinHandle
       where F: Fn(O) + Sync + Send + 'scope;

    // Provided method
    fn submit_all<T>(&self, iter: T)
       where T: IntoIterator<Item = I> { ... }
}
Expand description

Generic trait representing a thread pool.

See Threadpool and OrderedThreadpool for specific implementations.

Required Associated Types§

Source

type Iter<'a>: Iterator<Item = O> + 'a where Self: 'a

Source

type JoinHandle

Required Methods§

Source

fn submit(&self, input: I)

Synchronously submit a single job to the pool.

Source

fn recv(&self) -> O

Block the current thread until a result from the pool is available, and return it.

Source

fn try_recv(&self) -> Option<O>

Check if a result is available from the pool; if so, return it. If not, returns None.

Source

fn iter(&self) -> Self::Iter<'_>

Iterate over the currently available results in the pool.

Does not consume the pool; it only yields results until there are no jobs left to process. If more jobs are submitted to the pool afterwards, those results may be subsequently iterated over as well.

It’s recommended that you call GenericThreadpool::wait_until_finished before iterating, otherwise the cpu may be stuck in a busy wait while lingering jobs are still being processed.

Source

fn wait_until_finished(&self)

Block until all producers have been exhausted, and all workers have finished processing all jobs.

Source

fn producer<T>(&self, iter: T) -> Self::JoinHandle
where T: IntoIterator<Item = I> + Send + Sync + 'scope,

Spawn a new producer thread, which supplies jobs to the pool from the passed iterator asynchronously on a separate thread.

Source

fn consumer<F>(&self, f: F) -> Self::JoinHandle
where F: Fn(O) + Sync + Send + 'scope,

Spawn a new consumer thread, which consumes and processes results from the workers asynchronously on a separate thread.

Provided Methods§

Source

fn submit_all<T>(&self, iter: T)
where T: IntoIterator<Item = I>,

Synchronously submit many jobs to the pool at once from an iterator or collection.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'scope, 'env, I, O> GenericThreadpool<'scope, I, O> for OrderedThreadpool<'scope, 'env, I, O>
where I: Send + Sync + 'scope, O: Send + Sync + 'scope,

Source§

type Iter<'a> = OrderedThreadpoolIter<'a, 'scope, 'env, I, O> where Self: 'a

Source§

type JoinHandle = ScopedJoinHandle<'scope, ()>

Source§

impl<'scope, 'env, I, O> GenericThreadpool<'scope, I, O> for Threadpool<'scope, 'env, I, O>
where I: Send + Sync + 'scope, O: Send + Sync + 'scope,

Source§

type Iter<'a> = ThreadpoolIter<'a, 'scope, 'env, I, O> where Self: 'a

Source§

type JoinHandle = ScopedJoinHandle<'scope, ()>