[][src]Trait executors::common::Executor

pub trait Executor: Clone + Send {
    fn execute<F>(&self, job: F)
    where
        F: FnOnce() + Send + 'static
;
fn shutdown_async(&self);
fn shutdown_borrowed(&self) -> Result<(), String>; 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, 

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_borrowed(&self) -> Result<(), String>

Same as shutdown but without consuming self

Meant for use with trait object wrappers.

Loading content...

Provided methods

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

Implementors

impl Executor for executors::crossbeam_channel_pool::ThreadPool[src]

fn shutdown(self) -> Result<(), String>[src]

impl Executor for executors::crossbeam_workstealing_pool::ThreadPool[src]

fn shutdown(self) -> Result<(), String>[src]

impl Executor for RunNowExecutor[src]

fn shutdown(self) -> Result<(), String>[src]

impl Executor for ThreadPoolExecutor[src]

fn shutdown(self) -> Result<(), String>[src]

Loading content...