Created
August 22, 2016 11:09
-
-
Save KodrAus/157b0819028b4408419c8ab23920f37b to your computer and use it in GitHub Desktop.
Rust Crossbeam
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
extern crate crossbeam; | |
fn main() { | |
//Here we have a mutable array | |
let mut data = vec![1, 2, 3]; | |
println!("{:?}", data); | |
//With crossbeam magic we can mutate this array concurrently without locks | |
//The crossbeam scope guarantees that the threads inside it will end before the scope does | |
//That means it's safe to take references for our data, which must live longer than the thread | |
//We couldn't guarantee this in our previous example, so we ended up with locks | |
crossbeam::scope(|scope| { | |
for i in &mut data { | |
scope.spawn(move || { | |
*i += 1; | |
}); | |
} | |
}); | |
println!("{:?}", data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment