Created
July 27, 2015 00:44
-
-
Save sitag/848d3d2f13905de2c08e to your computer and use it in GitHub Desktop.
[rust] operating on a vector in parallel with unsafe rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(unique)] | |
use std::thread::spawn; | |
// operating on a vector in parallel | |
fn main() { | |
let mut data:Vec<u32> = vec![1u32, 2, 3]; | |
println!("{:?}", data); | |
let head = data.as_mut_ptr(); | |
let mut guards = (0..3).map(|i| | |
unsafe { | |
let mut target = std::ptr::Unique::new(head.offset(i)); | |
let guard = spawn(move || { | |
std::ptr::write(target.get_mut(), 10 + i as u32); | |
}); | |
guard | |
}); | |
if guards.all(|guard| match guard.join() { Ok(_) => true, Err(_) => false }) { | |
println!("{:?}", data); | |
} else { | |
println!("__COULD_NOT_JOIN_ALL__"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment