pub struct OrderedThreadpool<'scope, 'env, I, O> { /* private fields */ }
Expand description
An ordered thread pool.
Yields results in the same order they are submitted to the pool.
Retains much of the same functionality as a normal Threadpool
; refer to the documentation there
for more general information about the thread pools. It maintains many of the same
implementation details and limitations as well, so if not stated otherwise, assume the two pools
work in the same way.
Also like the Threadpool
, the OrderedThreadpool
has its own OrderedThreadpoolBuilder
,
which allows you to configure all the same options as you would be able to with a ThreadpoolBuilder
.
One implementation difference between this pool and the standard unordered Threadpool
is that
when passing a worker function to OrderedThreadpoolBuilder::build
, alongside
a thread id, the function also receives a monotonically increasing “job index” usize
value.
This value does not necessarily represent the original index of its corresponding input. It is simply
a monotonically increasing value unique among all inputs given to this threadpool. In the event that
this threadpool is used to only process a single iterator, the job index will
represent that index. However, subsequent iterators submitted to the pool will not maintain that property;
a new pool will have to be constructed if that is desired.
As this pool is guaranteed to preserve ordering of its inputs, it can be used to perform multithreaded map
and filter
operations on iterators passed to it. As such, it has an additional method
OrderedThreadpool::filter_map
. This works as you would expect, similar to its Iterator
counterpart,
Iterator::filter_map
. Some iterator extensions FilterMapMultithread::filter_map_multithread
and
FilterMapAsync::filter_map_async
are available to make this usage pattern even more seamless.
Of note, in exchange for yielding ordered outputs, results are no longer guaranteed to return immediately once they finish processing; there may be some delay, as results returned sooner than intended are placed inside a buffer, which also uses some additional amount of memory.
Implementations§
Source§impl<'scope, 'env, I, O> OrderedThreadpool<'scope, 'env, I, O>
impl<'scope, 'env, I, O> OrderedThreadpool<'scope, 'env, I, O>
Sourcepub fn new<F>(f: F, scope: &'scope Scope<'scope, 'env>) -> Self
pub fn new<F>(f: F, scope: &'scope Scope<'scope, 'env>) -> Self
Construct a new OrderedThreadpool
.
Provide a function that workers will use to process jobs,
and a Scope
that the pool will use to spawn worker threads
and any producer / consumer threads.
By default, the number of workers spawned is determined by the
result of available_parallelism
.
To specify the specific number of workers to spawn, and configure
several additional other custom properties including an initializer
and thread blocking properties, construct your pool via an
OrderedThreadpoolBuilder
instead.
Sourcepub fn filter_map<'a, T>(
&'a self,
iter: T,
) -> OrderedThreadpoolIter<'a, 'scope, 'env, I, O> ⓘ
pub fn filter_map<'a, T>( &'a self, iter: T, ) -> OrderedThreadpoolIter<'a, 'scope, 'env, I, O> ⓘ
Use this pool to perform a multithreaded filter_map
on the
passed iterator.
Equivalent to calling OrderedThreadpool::submit_all
, followed
by OrderedThreadpool::iter
.
Sourcepub fn filter_map_async<'a, T>(
&'a self,
iter: T,
) -> OrderedThreadpoolIter<'a, 'scope, 'env, I, O> ⓘ
pub fn filter_map_async<'a, T>( &'a self, iter: T, ) -> OrderedThreadpoolIter<'a, 'scope, 'env, I, O> ⓘ
Use this pool to perform a multithreaded filter_map
on the
passed iterator.
Passes the iterator up to a producer thread, so that results can be consumed from the pool immediately, even before the iterator finishes being exhausted.
Trait Implementations§
Source§impl<'scope, 'env, I, O> Extend<I> for OrderedThreadpool<'scope, 'env, I, O>
impl<'scope, 'env, I, O> Extend<I> for OrderedThreadpool<'scope, 'env, I, O>
Source§fn extend<T: IntoIterator<Item = I>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = I>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'scope, 'env, I, O> GenericThreadpool<'scope, I, O> for OrderedThreadpool<'scope, 'env, I, O>
impl<'scope, 'env, I, O> GenericThreadpool<'scope, I, O> for OrderedThreadpool<'scope, 'env, I, O>
Source§fn producer<T>(&self, iter: T) -> ScopedJoinHandle<'scope, ()>
fn producer<T>(&self, iter: T) -> ScopedJoinHandle<'scope, ()>
Spawn a new producer thread, which supplies jobs to the pool from the passed iterator asynchronously on a separate thread.
Because of the ordered nature of this pool, you probably don’t want to be submitting multiple jobs to the pool simultaneously from separate threads, as the results will become intermixed nondeterministically with each other. Nonetheless, even in such circumstances, the pool guarantees that the original ordering of the individual iterators will still be preserved, even if the specific interspersing of their individual elements is undefined and nondeterministic.
type Iter<'a> = OrderedThreadpoolIter<'a, 'scope, 'env, I, O> where Self: 'a
type JoinHandle = ScopedJoinHandle<'scope, ()>
Source§fn recv(&self) -> O
fn recv(&self) -> O
Source§fn try_recv(&self) -> Option<O>
fn try_recv(&self) -> Option<O>
None
.