Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Created August 22, 2016 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KodrAus/157b0819028b4408419c8ab23920f37b to your computer and use it in GitHub Desktop.
Save KodrAus/157b0819028b4408419c8ab23920f37b to your computer and use it in GitHub Desktop.
Rust Crossbeam
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