Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. // 2019-7-23
  2.  
  3. //use std::sync::mpsc::channel;
  4. use std::sync::{Mutex, Arc};
  5. extern crate rand;
  6. //use rand::prelude::*;
  7. //use rand::distributions::{Distribution, Uniform}; // 均匀分布
  8. //use std::time::{SystemTime};
  9.  
  10. extern crate rayon;
  11. //use rayon::prelude::*;
  12.  
  13.  
  14.  
  15.  
  16. fn main(){
  17. // 多线程Push vec, 需要Arc包装mutex
  18. let vecs:Vec<usize> = Vec::new();
  19. let mt = Arc::new(Mutex::new(vecs));
  20. // 两个线程 t1, t2 分别push
  21. let size = 20;
  22. let m = mt.clone();
  23. let n = mt.clone();
  24. let (_, _) = rayon::join(
  25. || {
  26. for i in 0..size{
  27. m.lock().unwrap().push(i as usize);
  28. }
  29. },
  30. || {
  31. for i in 2*size..3*size{
  32. n.lock().unwrap().push(i as usize);
  33. }
  34. },
  35. );
  36.  
  37. println!("Size = {}", size);
  38. mt.lock().unwrap()
  39. .chunks(20)
  40. .for_each(|x| println!("{:?}", x));
  41.  
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement