pub struct Executor { /* private fields */ }
Expand description
A multi-threaded executor.
Implementations§
Source§impl Executor
impl Executor
Sourcepub fn spawn<T: Send + 'static>(
&self,
future: impl Future<Output = T> + Send + 'static,
) -> Task<T> ⓘ
pub fn spawn<T: Send + 'static>( &self, future: impl Future<Output = T> + Send + 'static, ) -> Task<T> ⓘ
Sourcepub fn ticker(&self, notify: impl Fn() + Send + Sync + 'static) -> Ticker
pub fn ticker(&self, notify: impl Fn() + Send + Sync + 'static) -> Ticker
Creates a new ticker for executing tasks.
In a multi-threaded executor, each executor thread will create its own ticker and then keep
calling Ticker::tick()
in a loop.
§Examples
use blocking::block_on;
use multitask::Executor;
use std::thread;
let ex = Executor::new();
// Create two executor threads.
for _ in 0..2 {
let (p, u) = parking::pair();
let ticker = ex.ticker(move || u.unpark());
thread::spawn(move || {
loop {
if !ticker.tick() {
p.park();
}
}
});
}
// Spawn a future and wait for one of the threads to run it.
let task = ex.spawn(async { 1 + 2 });
assert_eq!(block_on(task), 3);
Trait Implementations§
impl RefUnwindSafe for Executor
impl UnwindSafe for Executor
Auto Trait Implementations§
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
Mutably borrows from an owned value. Read more