Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2019-7-23
- //use std::sync::mpsc::channel;
- use std::sync::{Mutex, Arc};
- extern crate rand;
- //use rand::prelude::*;
- //use rand::distributions::{Distribution, Uniform}; // 均匀分布
- //use std::time::{SystemTime};
- extern crate rayon;
- //use rayon::prelude::*;
- fn main(){
- // 多线程Push vec, 需要Arc包装mutex
- let vecs:Vec<usize> = Vec::new();
- let mt = Arc::new(Mutex::new(vecs));
- // 两个线程 t1, t2 分别push
- let size = 20;
- let m = mt.clone();
- let n = mt.clone();
- let (_, _) = rayon::join(
- || {
- for i in 0..size{
- m.lock().unwrap().push(i as usize);
- }
- },
- || {
- for i in 2*size..3*size{
- n.lock().unwrap().push(i as usize);
- }
- },
- );
- println!("Size = {}", size);
- mt.lock().unwrap()
- .chunks(20)
- .for_each(|x| println!("{:?}", x));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement