Trait executors::common::Executor
[−]
[src]
pub trait Executor: Clone + Send { fn execute<F>(&self, job: F)
where
F: FnOnce() + Send + 'static; fn shutdown_async(&self); fn shutdown(self) -> Result<(), String>; }
A common trait for task executors.
All implementations need to allow cloning to create new handles to the same executor, and they need to be safe to pass to threads.
Required Methods
fn execute<F>(&self, job: F) where
F: FnOnce() + Send + 'static,
F: FnOnce() + Send + 'static,
Executes the function job
on the Executor
.
Examples
Execute four jobs on an Executor
:
use executors::*; // initialise some executor executor.execute(|| println!("hello")); executor.execute(|| println!("world")); executor.execute(|| println!("foo")); executor.execute(|| println!("bar")); // wait for jobs to be executed std::thread::sleep(std::time::Duration::from_secs(1));
fn shutdown_async(&self)
Shutdown the Executor
without waiting.
This method can be used from one of the worker threads
(if the Executor
uses threads)
without risk of deadlocking.
Examples
Shutdown an Executor
with threads from within a worker.
use executors::*; // initialise some executor let executor2 = executor.clone(); executor.execute(|| println!("Hello!")); executor.execute(move || { println!("Shutting down"); executor2.shutdown_async(); }); std::thread::sleep(std::time::Duration::from_secs(1)); // or wait with a barrier executor.execute(|| println!("doesn't work!"));
fn shutdown(self) -> Result<(), String>
Shutdown an Executor
and wait for it to shut down all workers.
This method can be ONLY be used from outside the workers without risk of deadlocking.
Examples
Shutdown an Executor
with threads from an external thread.
use executors::*; // initialise some executor let executor2 = executor.clone(); executor.execute(|| println!("Hello!")); executor.shutdown().expect("pool to shut down"); executor2.execute(|| println!("doesn't work!"));
Implementors
impl Executor for RunNowExecutor
impl Executor for ThreadPool