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)
3fn main() {
4 procspawn::init();
5
6 let handle = spawn((), |()| {
7 panic!("Whatever!");
8 });
9
10 match handle.join() {
11 Ok(()) => unreachable!(),
12 Err(err) => {
13 let panic = err.panic_info().expect("got a non panic error");
14 println!("process panicked with {}", panic.message());
15 println!("{:#?}", panic);
16 }
17 }
18}
Additional examples can be found in: