pub fn spawn<A: Serialize + DeserializeOwned, R: Serialize + DeserializeOwned>(
args: A,
f: fn(_: A) -> R
) -> JoinHandle<R>
Expand description
Spawn a new process to run a function with some payload.
// call this early in your main() function. This is where all spawned
// functions will be invoked.
procspawn::init();
let data = vec![1, 2, 3, 4];
let handle = procspawn::spawn(data, |data| {
println!("Received data {:?}", &data);
data.into_iter().sum::<i64>()
});
let result = handle.join().unwrap();
Examples found in repository?
More examples
examples/panic.rs (lines 6-8)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
procspawn::init();
let handle = spawn((), |()| {
panic!("Whatever!");
});
match handle.join() {
Ok(()) => unreachable!(),
Err(err) => {
let panic = err.panic_info().expect("got a non panic error");
println!("process panicked with {}", panic.message());
println!("{:#?}", panic);
}
}
}
Additional examples can be found in: