pub struct CpuPool { /* private fields */ }
Expand description
A thread pool intended to run CPU intensive work.
This thread pool will hand out futures representing the completed work that happens on the thread pool itself, and the futures can then be later composed with other work as part of an overall computation.
The worker threads associated with a thread pool are kept alive so long as
there is an open handle to the CpuPool
or there is work running on them. Once
all work has been drained and all references have gone away the worker
threads will be shut down.
Currently CpuPool
implements Clone
which just clones a new reference to
the underlying thread pool.
Note: if you use CpuPool inside a library it’s better accept a
Builder
object for thread configuration rather than configuring just
pool size. This not only future proof for other settings but also allows
user to attach monitoring tools to lifecycle hooks.
Implementations§
Source§impl CpuPool
impl CpuPool
Sourcepub fn new(size: usize) -> CpuPool
pub fn new(size: usize) -> CpuPool
Creates a new thread pool with size
worker threads associated with it.
The returned handle can use execute
to run work on this thread pool,
and clones can be made of it to get multiple references to the same
thread pool.
This is a shortcut for:
Builder::new().pool_size(size).create()
§Panics
Panics if size == 0
.
Sourcepub fn new_num_cpus() -> CpuPool
pub fn new_num_cpus() -> CpuPool
Creates a new thread pool with a number of workers equal to the number of CPUs on the host.
This is a shortcut for:
Builder::new().create()
Sourcepub fn spawn<F>(&self, f: F) -> CpuFuture<F::Item, F::Error>
pub fn spawn<F>(&self, f: F) -> CpuFuture<F::Item, F::Error>
Spawns a future to run on this thread pool, returning a future representing the produced value.
This function will execute the future f
on the associated thread
pool, and return a future representing the finished computation. The
returned future serves as a proxy to the computation that F
is
running.
To simply run an arbitrary closure on a thread pool and extract the
result, you can use the future::lazy
combinator to defer work to
executing on the thread pool itself.
Note that if the future f
panics it will be caught by default and the
returned future will propagate the panic. That is, panics will not tear
down the thread pool and will be propagated to the returned future’s
poll
method if queried.
If the returned future is dropped then this CpuPool
will attempt to
cancel the computation, if possible. That is, if the computation is in
the middle of working, it will be interrupted when possible.
Sourcepub fn spawn_fn<F, R>(&self, f: F) -> CpuFuture<R::Item, R::Error>
pub fn spawn_fn<F, R>(&self, f: F) -> CpuFuture<R::Item, R::Error>
Spawns a closure on this thread pool.
This function is a convenience wrapper around the spawn
function above
for running a closure wrapped in future::lazy
. It will spawn the
function f
provided onto the thread pool, and continue to run the
future returned by f
on the thread pool as well.
The returned future will be a handle to the result produced by the
future that f
returns.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CpuPool
impl RefUnwindSafe for CpuPool
impl Send for CpuPool
impl Sync for CpuPool
impl Unpin for CpuPool
impl UnwindSafe for CpuPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)