Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[macro_use]
- extern crate lazy_static;
- use std::sync::{Arc, Mutex};
- use std::thread;
- use std::thread::sleep_ms;
- lazy_static! {
- static ref V: Vec<u32> = {
- let mut v = Vec::new();
- for i in 0..1_000_000 {
- v.push(i);
- }
- v
- };
- }
- fn main() {
- let a = Arc::new(Mutex::new(V.iter()));
- let mut threads = Vec::new();
- for _ in 0..4 {
- let a = a.clone();
- threads.push(thread::spawn(move || {
- /*while let Some(val) = a.lock().unwrap().next() {
- sleep_ms(1000);
- println!("{}", val);
- }*/
- loop {
- let step = a.lock().unwrap().next();
- match step {
- Some(val) => {
- sleep_ms(1000);
- println!("{}", val);
- },
- None => break
- }
- }
- }));
- }
- for th in threads.into_iter() {
- th.join();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement