Executor

Trait Executor 

Source
pub trait Executor:
    CanExecute
    + Clone
    + Send {
    // Required methods
    fn shutdown_async(&self);
    fn shutdown_borrowed(&self) -> Result<(), String>;

    // Provided methods
    fn execute<F>(&self, job: F)
       where F: FnOnce() + Send + 'static { ... }
    fn shutdown(self) -> Result<(), String> { ... }
}
Expand description

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§

Source

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

fn shutdown_borrowed(&self) -> Result<(), String>

Same as shutdown but without consuming self

Meant for use with trait object wrappers.

Provided Methods§

Source

fn execute<F>(&self, job: F)
where 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));
Source

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!"));

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 Executor for executors::crossbeam_channel_pool::ThreadPool

Source§

impl Executor for RunNowExecutor

Source§

impl Executor for ThreadPoolExecutor

Source§

impl<P> Executor for executors::crossbeam_workstealing_pool::ThreadPool<P>
where P: Parker + Clone + 'static,