Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.81 KB | None | 0 0
  1. #![feature(box_syntax)]
  2. #![feature(scoped)]
  3. use std::thread;
  4.  
  5. use std::sync::Arc;
  6. use std::sync::RwLock;
  7.  
  8. struct MyCell{
  9. a:f64,
  10. t:f64,
  11. }
  12. impl MyCell{
  13. fn new()->MyCell{
  14.         return MyCell{a:0.0,t:0.0};
  15. }
  16.  
  17. }
  18.  
  19. impl Copy for MyCell{}
  20. impl Clone for MyCell{
  21. fn clone(&self) -> MyCell{
  22.         return MyCell{a:self.a,t:self.t};
  23. }
  24. }
  25.  
  26. fn main() {
  27.         let lock1 = Arc::new(box [RwLock::new(box MyCell::new()),RwLock::new(box MyCell::new())]);
  28.  
  29.         let mut guards=Vec::with_capacity(10);
  30.         for _ in 0..10{
  31.         let g=thread::scoped(||{
  32.         let lock2 = lock1.clone();
  33.         let mut count=lock2[0].write().unwrap();
  34.         count.a+=1.0;} );
  35.         guards.push(g);
  36. }
  37.         for g in guards {
  38.         g.join();
  39. }
  40.     println!("Hello, world! {}",lock1[0].read().unwrap().a);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement