Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.77 KB | None | 0 0
  1. #[macro_use]
  2. extern crate lazy_static;
  3.  
  4. use std::sync::{Arc, Mutex};
  5. use std::thread;
  6. use std::thread::sleep_ms;
  7.  
  8. lazy_static! {
  9.     static ref V: Vec<u32> = {
  10.         let mut v = Vec::new();
  11.         for i in 0..1_000_000 {
  12.             v.push(i);
  13.         }
  14.         v
  15.     };
  16. }
  17.  
  18. fn main() {
  19.     let a = Arc::new(Mutex::new(V.iter()));
  20.     let mut threads = Vec::new();
  21.     for _ in 0..4 {
  22.         let a = a.clone();
  23.         threads.push(thread::spawn(move || {
  24.             /*while let Some(val) = a.lock().unwrap().next() {
  25.                 sleep_ms(1000);
  26.                 println!("{}", val);
  27.             }*/
  28.             loop {
  29.                 let step = a.lock().unwrap().next();
  30.                 match step {
  31.                     Some(val) => {
  32.                         sleep_ms(1000);
  33.                         println!("{}", val);
  34.                     },
  35.                     None => break
  36.                 }
  37.             }
  38.         }));
  39.     }
  40.     for th in threads.into_iter() {
  41.         th.join();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement