multithreading - Future::spawn() kinda useless? How can I multi-thread this? -
excerpt main
here:
let value: value = json::from_str(&sbuf).unwrap(); let coords = value.find("coordinates").unwrap().as_array().unwrap(); let x = future::spawn(|| coords.iter().fold(0f64, |mut a,b| { += read_coord_value(&b, "x"); })); let y = future::spawn(|| coords.iter().fold(0f64, |mut a,b| { += read_coord_value(&b, "y"); })); let z = future::spawn(|| coords.iter().fold(0f64, |mut a,b| { += read_coord_value(&b, "z"); })); println!("x: {}; y: {}; z: {}", x.await().unwrap(), y.await().unwrap(), z.await().unwrap());
so, basically, i'm doing here won't work because spawn call requires passed have static lifetime--which means there no work can avoid repeating. @ all. pointless.
what's good way threading here?
here, multithreading, need used scoped threads, std::thread::scoped(..)
.
these threads not need 'static
closure execute, must joined.
for example:
use std::thread::scoped; fn main() { let coords = [(1f64,2f64,3f64),(1.,2.,3.),(1.,2.,3.),(1.,2.,3.)]; let x = scoped(|| coords.iter().fold(0f64, |mut a,b| { += b.0; })); let y = scoped(|| coords.iter().fold(0f64, |mut a,b| { += b.1; })); let z = scoped(|| coords.iter().fold(0f64, |mut a,b| { += b.2; })); println!("x: {}; y: {}; z: {}", x.join(), y.join(), z.join()); }
Comments
Post a Comment