Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Last active August 22, 2016 11:39
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/124d2073dbefa5f7e9f0457b26c69372 to your computer and use it in GitHub Desktop.
Save KodrAus/124d2073dbefa5f7e9f0457b26c69372 to your computer and use it in GitHub Desktop.
Rust Concurrency
use std::sync::{ Arc, RwLock };
use std::thread;
fn main() {
//Here we have an immutable array with mutable interior cells
//The length of the array can't change, but internal values can
//We use the Send sync variants for threading
let data = Arc::new(vec![
RwLock::new(1),
RwLock::new(2),
RwLock::new(3)
]);
println!("{:?}", data);
//We spawn a thread for each value, acquire a lock and mutate it
let handles: Vec<thread::JoinHandle<()>> = (0..3)
.map(|i| {
//We need to get ownership of the data here
//This is so it doesn't try to move the data from the outer scope
//In the thread, we acquire a write lock guard to the cell and update
//The lock is released when the guard goes out of scope
let data = data.clone();
thread::spawn(move || {
let mut datum = data[i].write().unwrap();
*datum += 1;
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}
println!("{:?}", data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment