Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.83 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 mut v = Vec::new();
  20.     for i in 0..1_000_000 {
  21.         v.push(i);
  22.     }
  23.     let a = Arc::new(Mutex::new(V.iter()));
  24.     let mut threads = Vec::new();
  25.     for _ in 0..4 {
  26.         let a = a.clone();
  27.         threads.push(thread::spawn(move || {
  28.             /*while let Some(val) = a.lock().unwrap().next() {
  29.                 sleep_ms(1000);
  30.                 println!("{}", val);
  31.             }*/
  32.             loop {
  33.                 let step = a.lock().unwrap().next();
  34.                 match step {
  35.                     Some(val) => {
  36.                         sleep_ms(1000);
  37.                         println!("{}", val);
  38.                     },
  39.                     None => break
  40.                 }
  41.             }
  42.         }));
  43.     }
  44.     for th in threads.into_iter() {
  45.         th.join();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement