[][src]Function async_task::spawn

pub fn spawn<F, R, S, T>(
    future: F,
    schedule: S,
    tag: T
) -> (Task<T>, JoinHandle<R, T>) where
    F: Future<Output = R> + Send + 'static,
    R: Send + 'static,
    S: Fn(Task<T>) + Send + Sync + 'static,
    T: Send + Sync + 'static, 

Creates a new task.

This constructor returns a Task reference that runs the future and a JoinHandle that awaits its result.

When run, the task polls future. When woken, it gets scheduled for running by the schedule function. Argument tag is an arbitrary piece of data stored inside the task.

Examples

use crossbeam::channel;

// The future inside the task.
let future = async {
    println!("Hello, world!");
};

// If the task gets woken, it will be sent into this channel.
let (s, r) = channel::unbounded();
let schedule = move |task| s.send(task).unwrap();

// Create a task with the future and the schedule function.
let (task, handle) = async_task::spawn(future, schedule, ());