Function mitosis::spawn[][src]

pub fn spawn<F: FnOnce(A) -> R + Copy, A: Serialize + for<'de> Deserialize<'de>, R: Serialize + for<'de> Deserialize<'de>>(
    args: A,
    f: F
) -> JoinHandle<R>
Expand description

Spawn a new process to run a function with some payload

let data = vec![1, 2, 3, 4];
mitosis::spawn(data, |data| {
    // This will run in a separate process
    println!("Received data {:?}", data);
});

The function itself cannot capture anything from its environment, but you can explicitly pass down data through the args parameter. This function will panic if you pass a closure that captures anything from its environment.

The JoinHandle returned by this function can be used to wait for the child process to finish, and obtain the return value of the function it executed.

let data = vec![1, 1, 2, 3, 3, 5, 4, 1];
let handle = mitosis::spawn(data, |mut data| {
    // This will run in a separate process
    println!("Received data {:?}", data);
    data.dedup();
});
// do some other work
println!("Deduplicated {:?}", handle.join());