Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(box_syntax)]
- #![feature(scoped)]
- use std::thread;
- use std::sync::Arc;
- use std::sync::RwLock;
- struct MyCell{
- a:f64,
- t:f64,
- }
- impl MyCell{
- fn new()->MyCell{
- return MyCell{a:0.0,t:0.0};
- }
- }
- impl Copy for MyCell{}
- impl Clone for MyCell{
- fn clone(&self) -> MyCell{
- return MyCell{a:self.a,t:self.t};
- }
- }
- fn main() {
- let lock1 = Arc::new(box [RwLock::new(box MyCell::new()),RwLock::new(box MyCell::new())]);
- let mut guards=Vec::with_capacity(10);
- for _ in 0..10{
- let g=thread::scoped(||{
- let lock2 = lock1.clone();
- let mut count=lock2[0].write().unwrap();
- count.a+=1.0;} );
- guards.push(g);
- }
- for g in guards {
- g.join();
- }
- println!("Hello, world! {}",lock1[0].read().unwrap().a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement